|
-
Apr 27th, 2004, 02:01 PM
#1
Thread Starter
Addicted Member
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:
da = New OleDbDataAdapter("SELECT * FROM t", cnNew)
ds = New DataSet("t")
da.Fill(ds, "t")
For Each drRow In ds.Tables.Item("t").Rows
drRow.Item("LastN") = "test"
Next
ds.AcceptChanges()
Try
da.Update(ds, "t")
Catch ex As Exception
Trace.WriteLine(ex.Message)
End Try
-
Apr 27th, 2004, 03:29 PM
#2
Addicted Member
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
and try again.
Hope it will help.
-
Apr 27th, 2004, 03:42 PM
#3
Thread Starter
Addicted Member
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.
-
Apr 27th, 2004, 05:21 PM
#4
Addicted Member
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:
da = New OleDbDataAdapter("SELECT * FROM t", cnNew)
Dim cmdBdr As New OleDbCommandBuilder(da)
ds = New DataSet("t")
da.Fill(ds, "t")
For Each drRow In ds.Tables.Item("t").Rows
drRow.Item("LastN") = "test"
Next
'ds.AcceptChanges()
Try
da.Update(ds, "t")
Catch ex As Exception
Trace.WriteLine(ex.Message)
End Try
Good Luck!
-
Apr 27th, 2004, 05:38 PM
#5
Thread Starter
Addicted Member
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
-
Apr 27th, 2004, 06:19 PM
#6
Addicted Member
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:
da.UpdateCommand = myUpdateCommand
-
Apr 27th, 2004, 06:24 PM
#7
Addicted Member
As Carnifex rightly said, you need to define your update command.
-
Apr 27th, 2004, 06:47 PM
#8
Thread Starter
Addicted Member
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
-
Apr 27th, 2004, 08:19 PM
#9
Addicted Member
1. Create a OleDB command. Look on MSDN for how to do this.
e.g. If using SqL
VB Code:
'Define the update command
Dim myCommand As New SqlClient.SqlCommand("Update myTable Set Field1 = @Field1 Where Field2 = @Field2", myConnection)
'Create the Sql Parameters
Dim prmField1 As New SqlClient.SqlParameter("@Field1, SqlDbType.Int, 4, "Field1")
Dim prmField2 As New SqlClient.SqlParameter("@Field2, SqlDbType.Int, 4, "Field2")
'Add the parameters to the command
myCommand.Parameters.Add(prmField1)
myCommand.Parameters.Add(prmField2)
'Associate the command with the data adapter
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|