Grid Controls Committing Changes [modified]
-
I have some grid controls on a form that are using data adaptors to fill the grid directly from the SQL Server data base. I have added an even handler on exit of the form to commit changes but the changes are not getting written back to the data base. Here is my code - this.speciesTableAdapter.Fill(this.markingDBDataSet3.Species); private void MasterData_FormClosing(object sender, FormClosingEventArgs e) { SpeciesGrid.CommitEdit(DataGridViewDataErrorContexts.Commit); SpeciesGrid.Update(); } I must be missing something or I am totally on the wrong track. Any ideas? -- modified at 18:42 Monday 22nd May, 2006
-
I have some grid controls on a form that are using data adaptors to fill the grid directly from the SQL Server data base. I have added an even handler on exit of the form to commit changes but the changes are not getting written back to the data base. Here is my code - this.speciesTableAdapter.Fill(this.markingDBDataSet3.Species); private void MasterData_FormClosing(object sender, FormClosingEventArgs e) { SpeciesGrid.CommitEdit(DataGridViewDataErrorContexts.Commit); SpeciesGrid.Update(); } I must be missing something or I am totally on the wrong track. Any ideas? -- modified at 18:42 Monday 22nd May, 2006
Ok. To update the database based on changes you have made in the datagrid you must be calling the
DataAdapter.Update()
method. This takes two arguments: Your datatable/dataset (this will be the datasource of your grid), and (if you hand it a dataset) the name of the table. Ex.dAdapter.Update(somedataset, "Table");
ordAdapter.Update(sometable);
But you do need to specify an insert command in order for this to work. If a standard insert query is fine, you can use the command builder class. So, if you are using OleDb it would be like this:OleDbCommandBuilder cmdBuild = new OleDbCommandBuilder(dAdapter); dAdapter.Update(somedataset, "Table");
Hope that helps! ;P
-
Ok. To update the database based on changes you have made in the datagrid you must be calling the
DataAdapter.Update()
method. This takes two arguments: Your datatable/dataset (this will be the datasource of your grid), and (if you hand it a dataset) the name of the table. Ex.dAdapter.Update(somedataset, "Table");
ordAdapter.Update(sometable);
But you do need to specify an insert command in order for this to work. If a standard insert query is fine, you can use the command builder class. So, if you are using OleDb it would be like this:OleDbCommandBuilder cmdBuild = new OleDbCommandBuilder(dAdapter); dAdapter.Update(somedataset, "Table");
Hope that helps! ;P
I am using a SqlDatatAdaptor and when I try this solution, it builds without any errors but when the update is executed, I get the following exception error - Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is my code - SqlDataAdapter dAdapter = new System.Data.SqlClient.SqlDataAdapter(); dAdapter.Update(markingDBDataSet3, "Species");
-
I am using a SqlDatatAdaptor and when I try this solution, it builds without any errors but when the update is executed, I get the following exception error - Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is my code - SqlDataAdapter dAdapter = new System.Data.SqlClient.SqlDataAdapter(); dAdapter.Update(markingDBDataSet3, "Species");
You need to include the SqlCommandBuilder to make your update command for you:
SqlDataAdapter dAdapter = new SqlDataAdapter();
SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); // add this
dAdapter.Update(markingDBDataSet3, "Species");
Without this, the update will not work.
-
You need to include the SqlCommandBuilder to make your update command for you:
SqlDataAdapter dAdapter = new SqlDataAdapter();
SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); // add this
dAdapter.Update(markingDBDataSet3, "Species");
Without this, the update will not work.
I added the code as sugested - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.Update(markingDBDataSet3,"Species"); I am now getting a different error. Error - The DataAdapter.SelectCommand property needs to be initialized.
-
I added the code as sugested - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.Update(markingDBDataSet3,"Species"); I am now getting a different error. Error - The DataAdapter.SelectCommand property needs to be initialized.
Sean, you got me pointed in the right direction. I worked with one of the more experienced Windows developers here and we managed to cobble together the following which seems to be working. Evidently it needed a reference to the table adaptor and needed a select command added to the data adaptor. Here is the code - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.SelectCommand = new SqlCommand("select * from Species"); dAdapter.SelectCommand.Connection = speciesTableAdapter.Connection; dAdapter.Update(markingDBDataSet3,"Species"); Thank you very much for your help!:)
-
Sean, you got me pointed in the right direction. I worked with one of the more experienced Windows developers here and we managed to cobble together the following which seems to be working. Evidently it needed a reference to the table adaptor and needed a select command added to the data adaptor. Here is the code - SqlDataAdapter dAdapter = new SqlDataAdapter(); SqlCommandBuilder cmdBuild = new SqlCommandBuilder(dAdapter); dAdapter.SelectCommand = new SqlCommand("select * from Species"); dAdapter.SelectCommand.Connection = speciesTableAdapter.Connection; dAdapter.Update(markingDBDataSet3,"Species"); Thank you very much for your help!:)