C# ASP.NET Update database with datatable
Scenario: I'm just trying to update my database with the changes made by the user to their information. Here is my code:
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dt.Rows[0][2] = txtname.Text;
dt.Rows[0][3] = txtinterests.Text;
dt.Rows[0][4] = txtlocation.Text;
da.SelectCommand = new SqlCommand(sqlcommand, conn);
da.Update(dt);
I know its going to be something开发者_运维技巧 obvious, but what have I missed? There are no errors, everything compiles correctly, but nothing happens. The record remains unchanged.
You need to define the UpdateCommand on the dataadapter (possibly the InsertCommand too).
For each Modified row in the datatable, it will fire the command you specify as the UpdateCommand.
For each New row in the datatable, it will fire the command you specify as the InsertCommand.
Check out the MSDN reference here.
精彩评论