views:

148

answers:

1

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.

+1  A: 

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.

AdaTheDev
I added `da.UpdateCommand = new SqlCommand(sqlcommand, conn);` to the code. Do I need more than this? I've done this in the past and I'm sure its been very simple.
Sir Graystar
check to make sure your UpdateCommand is valid - can you post what the SQL is? Also, check your DataTable - step through to just double check you do have rows with a RowState of Modified (if you don't, then it won't be updating anything)
AdaTheDev
SQL is just `"SELECT * FROM Users`. Does this have to be something to do with update? The row state is Modified as well.
Sir Graystar
OK. For the UpdateCommand, you have to specify an "UPDATE" statement. Check out the link I posted above - that gives you a specific example code of what you need to do.
AdaTheDev
Thanks, got it working. The only problem I have is that the textboxes are not keeping their values. When i hit the save button, the text boxes revert back to their values from bwhen the page was loaded. The SQL runs, but updates with the old values.
Sir Graystar