Update changes made in gridview in DataTable
-
Hi, I have a sql database with data about customers. I made it possible to add/change/remove customers from the gridview. The customer's data are stored in a sql database. Suppose the user adds/changes/removes a row/cell in my gridview. How can i update the changes made in the gridview in the datatable? The idea behind all this is to update the sql database as much as possible 'automatically'. This is a peace of my code: dbCommand.CommandText = "SELECT firstName, LastName FROM Customer"; customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString); reader = dbCommand.ExecuteReader(); // Populate a new data table and bind it to the BindingSource. customerTable = new System.Data.DataTable(); customerAdapter.Fill(customerTable); grdcustomerManagement.DataSource = customerTable; Any idea's?
-
Hi, I have a sql database with data about customers. I made it possible to add/change/remove customers from the gridview. The customer's data are stored in a sql database. Suppose the user adds/changes/removes a row/cell in my gridview. How can i update the changes made in the gridview in the datatable? The idea behind all this is to update the sql database as much as possible 'automatically'. This is a peace of my code: dbCommand.CommandText = "SELECT firstName, LastName FROM Customer"; customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString); reader = dbCommand.ExecuteReader(); // Populate a new data table and bind it to the BindingSource. customerTable = new System.Data.DataTable(); customerAdapter.Fill(customerTable); grdcustomerManagement.DataSource = customerTable; Any idea's?
Use the same DataAdapter and call its Update method.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Use the same DataAdapter and call its Update method.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hi, I tried that, but it's giving me an error (which is in dutch) about 'delete command'. I used the update method of the data adapter in the Rowsremoved event of the gridview.
Ah. You need to create a CommandBuilder object on that DataAdapter so it can fill in the missing SQL insert, delete, and update queries that the adapter needs. Or, you could just add those Command objects to the DataAdapter yourself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Ah. You need to create a CommandBuilder object on that DataAdapter so it can fill in the missing SQL insert, delete, and update queries that the adapter needs. Or, you could just add those Command objects to the DataAdapter yourself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hi, Any chance you can give me a code sample? I've tried a few i found on the net. But no luck.
There is a single line of code you add to the creation of the DataAdapter to do this. I cannot believe you didn't find an example that explains this.
dbCommand.CommandText = "SELECT firstName, LastName FROM Customer";
customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString);
Dim cb = new SqlCommandBuilder(customerAdapter);
customerTable = new System.Data.DataTable();
customerAdapter.Fill(customerTable);A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
There is a single line of code you add to the creation of the DataAdapter to do this. I cannot believe you didn't find an example that explains this.
dbCommand.CommandText = "SELECT firstName, LastName FROM Customer";
customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString);
Dim cb = new SqlCommandBuilder(customerAdapter);
customerTable = new System.Data.DataTable();
customerAdapter.Fill(customerTable);A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Hi, I was actually talking about how to update the datatable after a few rows have been removed. I already got those lines in my application. I tried updating the datatable with customerAdapter.Update() method. But it's complaining about a deleteCommand. So after some rows have been removed from the gridview which i caught in the event handler of the datagridview, how do i update the datatable and update the sql database with the customerAdapter.Update() method? [EDIT] Ah never mind, i see what i was doing wrong. I forgot to put the adapter inside the () of the commandbuilder instance. Thank you, it works perfectly now! [/EDIT]
modified on Friday, December 12, 2008 2:37 AM