By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,065 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,065 IT Pros & Developers. It's quick & easy.

Failed to update datarow

P: n/a
Hello,

I tried to update a row in a table by using the datarow and data
adapter. Unfortunately, it doesn't update.

I try finding that datarow and set it to "dr", I can see dr could be
just an instance of the row, but aren't linked to the table. Is that
why it doesn't update? If not, what is missing? Please help!!!

Dim da As New SqlDataAdapter("select * from Table1", cn)
Dim ds As New DataSet()
Dim dr As DataRow
da.Fill(ds, "Table1")
Dim keys(0) As DataColumn
keys(0) = ds.Tables(Table1").Columns("ID")
ds.Tables("Table1").PrimaryKey = keys
dr = ds.Tables("Table1").Rows.Find (1)
dr.BeginEdit()

dr("F1") = "xxx"
dr.EndEdit()
dr.AcceptChanges()
da.Update(ds, "Table1")
Thanks in advance.
Wanda
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Wanda wrote:
Hello,

I tried to update a row in a table by using the datarow and data
adapter. Unfortunately, it doesn't update.

I try finding that datarow and set it to "dr", I can see dr could be
just an instance of the row, but aren't linked to the table. Is that
why it doesn't update? If not, what is missing? Please help!!!

Dim da As New SqlDataAdapter("select * from Table1", cn)
Dim ds As New DataSet()
Dim dr As DataRow
da.Fill(ds, "Table1")
Dim keys(0) As DataColumn
keys(0) = ds.Tables(Table1").Columns("ID")
ds.Tables("Table1").PrimaryKey = keys
dr = ds.Tables("Table1").Rows.Find (1)
dr.BeginEdit()

dr("F1") = "xxx"
dr.EndEdit()
dr.AcceptChanges()
da.Update(ds, "Table1")
Thanks in advance.
Wanda


I think you have to do da.Update before dr.AcceptChanges.
After AcceptChanges your DataRow is considered to be the original one so

it won't lead to an update
Nov 21 '05 #2

P: n/a
Remove the AcceptChanges(), if you leave it in, there will be nothing to
update as this sets the new or updated rows to the original rows and the
dataAdapter has nothing to work with.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"Wanda" <wa*****@rogers.com> wrote in message
news:11**************************@posting.google.c om...
Hello,

I tried to update a row in a table by using the datarow and data
adapter. Unfortunately, it doesn't update.

I try finding that datarow and set it to "dr", I can see dr could be
just an instance of the row, but aren't linked to the table. Is that
why it doesn't update? If not, what is missing? Please help!!!

Dim da As New SqlDataAdapter("select * from Table1", cn)
Dim ds As New DataSet()
Dim dr As DataRow
da.Fill(ds, "Table1")
Dim keys(0) As DataColumn
keys(0) = ds.Tables(Table1").Columns("ID")
ds.Tables("Table1").PrimaryKey = keys
dr = ds.Tables("Table1").Rows.Find (1)
dr.BeginEdit()

dr("F1") = "xxx"
dr.EndEdit()
dr.AcceptChanges()
da.Update(ds, "Table1")
Thanks in advance.
Wanda

Nov 21 '05 #3

P: n/a
It doesn't work either if I do da.Update before dr.AcceptChanges,
instead I now get the following error message:

Update requires a valid UpdateCommand when passed DataRow collection
with modified rows.

Thanks
Wanda


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #4

P: n/a
Yes, you need to give the DataAdapter a valid UpdateCommand, you need the
following Commands generally.
SelectCommand
DeleteCommand
InsertCommand
UpdateCommand

Yours will be empty, this is why you are getting the error message.
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"Wanda Li" <wa*****@rogers.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
It doesn't work either if I do da.Update before dr.AcceptChanges,
instead I now get the following error message:

Update requires a valid UpdateCommand when passed DataRow collection
with modified rows.

Thanks
Wanda


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #5

P: n/a
Do you mean I have to use

"Update Table1 Set F1 = 'xxx'" ???

Can I do something as simple as

dr.BeginEdit
dr("f1") = "xxx"
dr.EndEdit

then update???

I don't want to use the update statement since I have so many fields to
update, even I know I can do it in one update statement.

Thanks
Wanda


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

P: n/a
Wanda,

You can use the commandbuilder for this.

da.Fill(ds, "Table1")
Dim keys(0) As DataColumn
keys(0) = ds.Tables(Table1").Columns("ID")
ds.Tables("Table1").PrimaryKey = keys
dr = ds.Tables("Table1").Rows.Find (1)
dr.BeginEdit()
dr("F1") = "xxx"
dr.EndEdit()
dim cmb as New xxxCommandBuilder(da) 'xxx = SQL or OleDb
da.Update(ds, "Table1")

'the acceptchanges is normally done by the dataadapter.

(The commandbuilder will not work with complex selectstatements by instance
when there are more than 100 items in a datarow)

I hope this helps?

Cor

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.