Results 1 to 9 of 9

Thread: can't update table

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    didn't decide yet
    Posts
    222

    can't update table

    i m using the following code without errors but the data in the table remains the same what i m doing wrong

    VB Code:
    1. da = New OleDbDataAdapter("SELECT * FROM t", cnNew)
    2.         ds = New DataSet("t")
    3.         da.Fill(ds, "t")
    4.         For Each drRow In ds.Tables.Item("t").Rows
    5.             drRow.Item("LastN") = "test"
    6.         Next
    7.         ds.AcceptChanges()
    8.         Try
    9.             da.Update(ds, "t")
    10.         Catch ex As Exception
    11.             Trace.WriteLine(ex.Message)
    12.         End Try
    Come and get our ISDN CallerID http://www.3wm.biz

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    what about the update command for the data adaptor?
    you don't need to call the AcceptChanges() method of the data adaptor before the update. in order words comment out

    VB Code:
    1. ds.AcceptChanges()

    and try again.

    Hope it will help.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    didn't decide yet
    Posts
    222
    i have used that line cuase else i m getting the following error

    Code:
    Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
    Come and get our ISDN CallerID http://www.3wm.biz

  4. #4
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    u need an update command to update the data store. u can either write an SQL command for update and assign it to the UpdateCommand property of the data adaptor. If new rows will be added, then u need an InsertCommand. if rows will be deleted, you need a DeleteCommand.

    Alternatively, you can use the CommandBuilder. You pass it a data adaptor object with a valid select command and it builds Insert, Delete and Update commands for you.

    TRY THIS
    VB Code:
    1. da = New OleDbDataAdapter("SELECT * FROM t", cnNew)
    2. Dim cmdBdr As New OleDbCommandBuilder(da)
    3. ds = New DataSet("t")
    4. da.Fill(ds, "t")
    5. For Each drRow In ds.Tables.Item("t").Rows
    6.      drRow.Item("LastN") = "test"
    7. Next
    8. 'ds.AcceptChanges()
    9. Try
    10.     da.Update(ds, "t")
    11. Catch ex As Exception
    12.     Trace.WriteLine(ex.Message)
    13. End Try

    Good Luck!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    didn't decide yet
    Posts
    222
    thnks robymix but i m very unlucky my update table does not have any key and when i tried your solution i m getting an error that i key is required do u have a solution for that ?

    ps:except adding a primary field key
    Come and get our ISDN CallerID http://www.3wm.biz

  6. #6
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    You need to explicitly define your update command. You cannot use command builder as you do not have any primary columns. (Using command builder is bad for performance anyway)

    Check out MSDN on OleDb.OleDbCommand and OleDb.OleDbParameter.

    Once you have defined your command add it to the adapter
    VB Code:
    1. da.UpdateCommand = myUpdateCommand

  7. #7
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    As Carnifex rightly said, you need to define your update command.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    didn't decide yet
    Posts
    222
    that isn't so straight forward.

    "define an update command"

    what do u mean how can i keep trace of how many records i have updated!!!

    could u be a little more clear on that also please
    Come and get our ISDN CallerID http://www.3wm.biz

  9. #9
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    1. Create a OleDB command. Look on MSDN for how to do this.
    e.g. If using SqL

    VB Code:
    1. 'Define the update command
    2. Dim myCommand As New SqlClient.SqlCommand("Update myTable Set Field1 = @Field1 Where Field2 = @Field2", myConnection)
    3.  
    4. 'Create the Sql Parameters
    5. Dim prmField1 As New SqlClient.SqlParameter("@Field1, SqlDbType.Int, 4, "Field1")
    6. Dim prmField2 As New SqlClient.SqlParameter("@Field2, SqlDbType.Int, 4, "Field2")
    7.  
    8. 'Add the parameters to the command
    9. myCommand.Parameters.Add(prmField1)
    10. myCommand.Parameters.Add(prmField2)
    11.  
    12. 'Associate the command with the data adapter
    13. da.UpdateCommand = myCommand

    2. Attach it to the DataAdapter how I showed in my example code.
    3. The data adapter will still return how many records were updated when update is called. If you want to use the adapter to add or delete records you must also define the insert and delete commands.

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