Results 1 to 23 of 23

Thread: Database Update...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Database Update...

    Hello, I can't update my database. Why??? I found code snippets in thius forum, mine is the same. Here's the code:
    VB Code:
    1. Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" _
    2. & "c:\mypath\db.mdb" ' Put here the connection string
    3. Dim myConnection As New OleDbConnection(myConnString) 'Create a new connection
    4.  
    5. Dim MyDataSet As New DataSet()
    6. Dim MyDataAdapter As New OleDbDataAdapter("SELECT * from MyTable", myConnection)
    7. MyDataAdapter.Fill(MyDataSet, "MyTable")
    8. MyDataSet.Tables(0).Rows(0).Item(2) = 123412 'modify 3nd column of first row
    9. MyDataAdapter.Update(MyDataSet) 'Here's the error
    The error is: "Update was not able to find TableMapping['Table'] or DataTable 'Table'".

    Thnx
    Xmas
    Learn, this is the Keyword...

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    How many tables do you have in the database ?

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this

    VB Code:
    1. 'instead of the one you wrote
    2. MyDataAdapter.Update(MyDataSet,"MyTable")

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Got another error:
    "Update needs a valid UpdateCommand if DataRow collection is passed with modified rows"
    What's that?

    Thx
    Learn, this is the Keyword...

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Where's the error ?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    In the same place:

    MyDataAdapter.Update(MyDataSet,"MyTable")

    I have 10 tables in my DB, but I tried with a new DB with only 1 table. Nothing to do

    Pirate, any ideas?
    Tx
    Learn, this is the Keyword...

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok , are you updating existing data or adding new data ?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    I'm updating, not adding. I tried without modifying the content of the table, and everything works. But when I change something in the dataset I get this error. I didn't try adding a new row yet.
    Learn, this is the Keyword...

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Do you have a valid update command for your dataadapter?
    It should look something like this:
    VB Code:
    1. Me.myDataAdapter.UpdateCommand = Me.OleDbUpdateCommand1
    2. Me.OleDbUpdateCommand1.Connection = myConnection
    3. Me.OleDbUpdateCommand1.CommandText = "UPDATE myTable SET myField= ? where (myField = ?)"
    4. Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("myField", System.Data.OleDb.OleDbType.VarWChar, 50, "myField"))
    5. Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_myField", System.Data.OleDb.OleDbType.VarWChar, 50,  _
    6. System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "myField", System.Data.DataRowVersion.Original, Nothing))


    Edits: Some Spelling Errors
    Last edited by Lunatic3; Apr 27th, 2003 at 12:50 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I sounds you are missing Update sql command . I've not tried your method to update existing data but I only use that when I add new data .

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Lunatic3 was a bit faster...

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    In case you need to insert data to table you also need a valid Insert command.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  13. #13
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can let VS.NET build an update command for you. Just declare a commandbuilder object and pass it to your dataadapter.
    Dont gain the world and lose your soul

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Thumbs up

    Wow! Looking at Lunatic3's code it seems to me very complicated, but with commandbuilder, DevGrp's trick, it works great!!!
    Thank you all, guys!

    Xmas
    Learn, this is the Keyword...

  15. #15
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    The command builder just automates that process for you and the idea is basically the same.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Sure, but i'm not so strong with SQL
    Learn, this is the Keyword...

  17. #17
    New Member
    Join Date
    Apr 2003
    Posts
    7
    Hi,

    I also can't complete the database update routine.
    I wanted to bind a dataset to a datagrid. Then to modify data in the datagrid, and on "Save" click to update a table.

    'To populate the grid:
    Dim da As New OleDbDataAdapter("Select * From Table1", cnn)
    Dim ds As New DataSet()
    da.Fill(ds, "Table1")
    da.Dispose()
    cnn.Close()
    dGrid.SetDataBinding(ds, "")

    'On Save click:
    Dim cnn As New OleDbConnection(ConnString)
    cnn.Open()
    Dim da As New OleDbDataAdapter("Select * From Table1", cnn)
    Dim cb As New OleDbCommandBuilder(da)
    da.Update(ds.Tables("Table1"))

    This code allows me to delete rows from the table, and to edit a specific columns, but I can't add new records. I'm getting "Error in the INSERT Statement"

    Any ideas why?

    Thanks

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Originally posted by Lunatic3
    In case you need to insert data to table you also need a valid Insert command.
    I think this is the reason. Try to add
    VB Code:
    1. cb.getinsertcommand() ' I think this is the method, now I can't check it...
    Another question: I noticed that I can select only ONE table at time. In fact, if I select 2 or more tables, i.e. "SELECT * FROM table1,table2", it returns me only "</newdataset>". Any ideas ?
    Learn, this is the Keyword...

  19. #19
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    it returns me only "</newdataset>".
    What you mean by that?
    You can select from multiple tables in one dataadapter, however they will be mapped to only one table in the dataset.

    Edits:
    cb.getinsertcommand() ' I think this is the method, now I can't check it...
    By the way your method you mentioned just returns the insert statment(does not produce one, its produced already) so he can check what may be wrong with it. More detail of error may be helpful.
    Last edited by Lunatic3; Apr 28th, 2003 at 05:32 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    VB Code:
    1. Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" _
    2. & "c:\mypath\db.mdb" ' Put here the connection string
    3. Dim myConnection As New OleDbConnection(myConnString) 'Create a new connection
    4.  
    5. Dim MyDataSet As New DataSet()
    6. Dim MyDataAdapter As New OleDbDataAdapter("SELECT * from MyTable1,MyTable2", myConnection) 'Select from 2 tables
    7. MyDataAdapter.Fill(MyDataSet)
    8. MsgBox(MyDataSet.GetXml()) ' MsgBox's message is only "</newdataset>"
    If I remove MyTable2 (or MyTable1) I get the real content of my database.

    In my code I used this:
    VB Code:
    1. Mydataadapter.insert.textcommand=cb.getinsertcommand()
    and it worked.
    Learn, this is the Keyword...

  21. #21
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Look, you can select from two table, but as far as you dont define any logical relationship (Inner join, outer join...) between them then you will have a table that will contain ALL of the fields of both tables and ... (give it a try ..)

    So if you want to have two separate tables you have to define tow seprate dataaadapters, but still you can map that adapaters to one Dataset and your dataset will have two tables.
    Mydataadapter.insert.textcommand=cb.getinsertcommand()
    and it worked.
    It would have worked without that too. All you have to do is defining a commandbuilder for a dataadapter. It does the job automatically.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Thanks, I'll do in that way, but I can't still understand why it returns me a blank dataset instead of a dataset with one big table with all the columns.

    Xmas.
    Learn, this is the Keyword...

  23. #23
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Yes, its strange
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width