Update requires a valid UpdateCommand when passed DataRow collection with modified rows. [modified]
-
Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
I am writing the code here.private void btnInsert\_Click(object sender, EventArgs e) { sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error. } private void Form1\_Load(object sender, EventArgs e) { dataGridView1.DataSource = bindingSource; PopulateDataGridView(); } private void PopulateDataGridView() { try { String sqlCommand = "select \* from " + tableName; sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter); DataSet dsData = new DataSet(); dsData.Locale = CultureInfo.InvariantCulture; sqlDataAdapter.Fill(dsData); bindingSource.DataSource=dsData.Tables\[0\]; } catch (SqlException sqlException) { MessageBox.Show(sqlException.Message.ToString()); } }
Everything is working fine but when I click on the Insert Button it gives me error. Please Help.
modified on Wednesday, October 8, 2008 6:19 AM
-
Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
I am writing the code here.private void btnInsert\_Click(object sender, EventArgs e) { sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error. } private void Form1\_Load(object sender, EventArgs e) { dataGridView1.DataSource = bindingSource; PopulateDataGridView(); } private void PopulateDataGridView() { try { String sqlCommand = "select \* from " + tableName; sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter); DataSet dsData = new DataSet(); dsData.Locale = CultureInfo.InvariantCulture; sqlDataAdapter.Fill(dsData); bindingSource.DataSource=dsData.Tables\[0\]; } catch (SqlException sqlException) { MessageBox.Show(sqlException.Message.ToString()); } }
Everything is working fine but when I click on the Insert Button it gives me error. Please Help.
modified on Wednesday, October 8, 2008 6:19 AM
Try setting UpdateCommand property of SqlDataAdapter class manually.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
I am writing the code here.private void btnInsert\_Click(object sender, EventArgs e) { sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error. } private void Form1\_Load(object sender, EventArgs e) { dataGridView1.DataSource = bindingSource; PopulateDataGridView(); } private void PopulateDataGridView() { try { String sqlCommand = "select \* from " + tableName; sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter); DataSet dsData = new DataSet(); dsData.Locale = CultureInfo.InvariantCulture; sqlDataAdapter.Fill(dsData); bindingSource.DataSource=dsData.Tables\[0\]; } catch (SqlException sqlException) { MessageBox.Show(sqlException.Message.ToString()); } }
Everything is working fine but when I click on the Insert Button it gives me error. Please Help.
modified on Wednesday, October 8, 2008 6:19 AM
listen, i can´t debug that, because the way i code is different. :doh: Try this routine, may work out for you.
// connection string
public static string connection_string = "..."
// store procedure
public static string SP_whatever = "...";// database connection (no parameters)
public static void DataGridView_Fill(DataGridView dgv, string store_procedure)
{
SqlConnection sql_conn = new SqlConnection(connection_string);
SqlCommand sql_cmd = new SqlCommand();
sql_cmd.Connection = sql_conn;
sql_cmd.CommandText = store_procedure;
sql_cmd.CommandType = CommandType.StoredProcedure;
sql_conn.Open();
SqlDataAdapter sql_da = new SqlDataAdapter();
sql_da.SelectCommand = sql_cmd;
DataTable dt = new DataTable();
sql_da.Fill(dt);
sql_conn.Close();
dgv.DataSource = dt;
}nelsonpaixao@yahoo.com.br trying to help & get help