Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have filled my datatable with my sqladapter associated with my sqlcommand and sqlconnection. I have changed its contents by a textbox bound to one of its column. I want to save the data in it back into the database and I have done as the following but it doesn't work:
C#
mydatatable.Merge(mydatatable.GetChanges());
mydatatable.AcceptChanges();
mydataadapter.Update(mydatatable);

Of course, it doesn't check if there is any errors in the new changes but suppose there's not any error.
Could you please correct my code? Why doesn't it work? If using such as a datagrid, I can easily update the data using DataAdapter.Update method.
Thank you so much!
Posted
Updated 10-Sep-11 12:08pm
v2
Comments
Pradeep Shukla 10-Sep-11 17:42pm    
The code seems to be correct, What is the exception you got?
Simon Bang Terkildsen 10-Sep-11 18:21pm    
He doesn't get an exception, nothing is commited to the database, because he calls AcceptChanges.
Actually the first two lines in the three lines of posted code must be deleted. See my answer.

1 solution

1) Merging changes from mydatatable into the very same mydatatable makes no sense, nothing will change, so it's not an error, just a waste of CPU cycles. So I suggest you remove mydatatable.Merge(mydatatable.GetChanges());

2) mydatatable.AcceptChanges(); calling AcceptChanges will set the state of all rows in the DataTable to Unchanged. So after this call there are no changes to commit to a database.
Remove mydatatable.AcceptChanges(); and your code should work, assuming you've set the UpdateCommand and InsertCommand of the SqlDataAdapter either yourself or by using OleDbCommandBuilder[^]. If you haven't then have a look at this:
How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual C# .NET[^]
It states it's for SQL Server but that's just a matter of difference in the SQLs the code is the same no matter which database you use.
 
Share this answer
 
Comments
[no name] 11-Sep-11 6:18am    
As I said, I can easily update when editing my data through a datagridview but not when editing my data through a textbox. Both are bound to the datatable and can change the data in the datatable.
I have removed all redundant codes as you suggested and used only dataadapter.Update, of course after creating a SqlCommandBuilder for the adapter, but the data couldn't be updated.
Do you have any idea?
Thank you so much!
Simon Bang Terkildsen 11-Sep-11 12:34pm    
Do you get any exception or is your data just not inserted/updated? If you do not get an exception set a break point at mydataadapter.Update(mydatatable); and and inspects the mydatatable.Rows collection to make sure the rows have the current RowState, rows that should be updated has the RowState Modified and rows that should be inserted has the RowState Added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900