record no update
-
Hi. i face a problem, record not update and no error show please check my coding and tell me where i do mistake my coding is private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); conn.Open(); cmd.CommandText = "update contract set suppliername='" + dataGridView1.CurrentRow.Cells[1] + "',buyername='" + dataGridView1.CurrentRow.Cells[2] + "',contract_dt='" + dataGridView1.CurrentRow.Cells[3] + "',delivery='" + dataGridView1.CurrentRow.Cells[4] + "',pmode='" + dataGridView1.CurrentRow.Cells[5] + "',comm='" + dataGridView1.CurrentRow.Cells[6] + "',commper='" + dataGridView1.CurrentRow.Cells[7] + "' where contractid='" + dataGridView1.CurrentRow.Cells[0] + "'"; cmd.Connection = conn; cmd.ExecuteNonQuery(); MessageBox.Show("Record Update.."); conn.Close(); }
-
Hi. i face a problem, record not update and no error show please check my coding and tell me where i do mistake my coding is private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); conn.Open(); cmd.CommandText = "update contract set suppliername='" + dataGridView1.CurrentRow.Cells[1] + "',buyername='" + dataGridView1.CurrentRow.Cells[2] + "',contract_dt='" + dataGridView1.CurrentRow.Cells[3] + "',delivery='" + dataGridView1.CurrentRow.Cells[4] + "',pmode='" + dataGridView1.CurrentRow.Cells[5] + "',comm='" + dataGridView1.CurrentRow.Cells[6] + "',commper='" + dataGridView1.CurrentRow.Cells[7] + "' where contractid='" + dataGridView1.CurrentRow.Cells[0] + "'"; cmd.Connection = conn; cmd.ExecuteNonQuery(); MessageBox.Show("Record Update.."); conn.Close(); }
If you are not sure what is going on, then enclose that code in a try...catch block and use
MessageBox.Show(ex.ToString());
to find out. It may be that you have a try...catch block outside this method which is catching and discarding the error. On a related note: change this to use parameterized queries, you are leaving yourself wide open to a SQL Injection attack. (Google for "Bobby Tables" to find out what that is) P.S. When you give code fragments, always enclose them in <pre> </pre> blocks with the "code block" widget to preserve the formatting.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Hi. i face a problem, record not update and no error show please check my coding and tell me where i do mistake my coding is private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); conn.Open(); cmd.CommandText = "update contract set suppliername='" + dataGridView1.CurrentRow.Cells[1] + "',buyername='" + dataGridView1.CurrentRow.Cells[2] + "',contract_dt='" + dataGridView1.CurrentRow.Cells[3] + "',delivery='" + dataGridView1.CurrentRow.Cells[4] + "',pmode='" + dataGridView1.CurrentRow.Cells[5] + "',comm='" + dataGridView1.CurrentRow.Cells[6] + "',commper='" + dataGridView1.CurrentRow.Cells[7] + "' where contractid='" + dataGridView1.CurrentRow.Cells[0] + "'"; cmd.Connection = conn; cmd.ExecuteNonQuery(); MessageBox.Show("Record Update.."); conn.Close(); }
The values you try to add to your ugly dynamic SQL are of type
DataGridViewCell
So first of all, usedataGridView1.CurrentRow.Cells[x].Value
to extract the values and as OriginalGriff already said, use parameterized queries. It's easier, more readable, more efficient and NOT UGLY.var question = (_2b || !(_2b));