Trying to update data in a SQL Server Table from a DataGrid

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif This is maddening.<span>
I’m trying to update data in a SQL Server Table from a DataGrid.<span>
Here’s my code:
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue private <span style="color:blue void button4_Click(<span style="color:blue object sender,
<span style="color:#2B91AF EventArgs e)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:#2B91AF DataTable dataGridView1 = <span style="color:blue
new <span style="color:#2B91AF DataTable(<span style="color:#A31515 "Import_List");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
dataGridView1.DataSource = dataGridView1;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:#2B91AF SqlConnection connection = <span style="color:blue
new <span style="color:#2B91AF SqlConnection(<span style="color:#A31515 "Data Source=Excel-PC;Initial Catalog=Northwind.MDF;Trusted_Connection=True;");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:#2B91AF SqlCommand cmd = <span style="color:blue
new <span style="color:#2B91AF SqlCommand(<span style="color:#A31515 "Insert into Import_List (Field1,Field2,Field3) Values (@f1,@f2,@f3)", connection);
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters.AddWithValue(<span style="color:#A31515 "@f1", <span style="color:#A31515
"");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters.AddWithValue(<span style="color:#A31515 "@f2", <span style="color:#A31515
"");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters.AddWithValue(<span style="color:#A31515 "@f3", <span style="color:#A31515
"");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue foreach (<span style="color:#2B91AF DataRow dataRow
<span style="color:blue in dataGridView1.Rows)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue using (connection)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
connection.Open();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters[0].Value = dataRow[1].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters[1].Value = dataRow[2].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.Parameters[2].Value = dataRow[3].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
cmd.ExecuteNonQuery();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif The problem is definitely on this line:
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif dataGridView1.DataSource = dataGridView1;
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif WindowsFormsApplication6.Form1 does not contain a definition for dataGridView1_CellContentClick and no extension method dataGridView1_CellContentClick
accepting a first argument of type WindowsFormsApplication6.Form1 could be found (are you missing a using directive or an assembly reference?)
<p style="margin-bottom:0in; margin-bottom:.0001pt <span style="font-family:Times New Roman,serif
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif The name of the DataGrid is ‘dataGridView1’ <hr class="sig Ryan Shuell

View the full article
 
Back
Top