problem with updating records from datagrid
-
i m editing records through datagrid..behind my update button i've written following code.. String str = " "; for (int i = 0; i == editgrid.RowCount - 1; i++) { str = "UPDATE criminal SET cr_id = " + editgrid.Rows[i].Cells[1].Value + ", cr_name= '" + editgrid.Rows[i].Cells[2].Value + "', cr_desc='" + editgrid.Rows[i].Cells[3].Value + "' ,cr_pic= '" + editgrid.Rows[i].Cells[4].Value + "', no_times ='" + editgrid.Rows[i].Cells[5].Value + "' WHERE cr_id = " + editgrid.Rows[i].Cells[1].Value; SqlCommand com = new SqlCommand(str, conn); com.ExecuteNonQuery(); label2.Visible = true; } it is not giving any errors but records are not updated,yet can anybody help me??
software student
-
i m editing records through datagrid..behind my update button i've written following code.. String str = " "; for (int i = 0; i == editgrid.RowCount - 1; i++) { str = "UPDATE criminal SET cr_id = " + editgrid.Rows[i].Cells[1].Value + ", cr_name= '" + editgrid.Rows[i].Cells[2].Value + "', cr_desc='" + editgrid.Rows[i].Cells[3].Value + "' ,cr_pic= '" + editgrid.Rows[i].Cells[4].Value + "', no_times ='" + editgrid.Rows[i].Cells[5].Value + "' WHERE cr_id = " + editgrid.Rows[i].Cells[1].Value; SqlCommand com = new SqlCommand(str, conn); com.ExecuteNonQuery(); label2.Visible = true; } it is not giving any errors but records are not updated,yet can anybody help me??
software student
Print your query somewhere and execute it directly in query analyzer. You can find out what is wrong with query.
-
i m editing records through datagrid..behind my update button i've written following code.. String str = " "; for (int i = 0; i == editgrid.RowCount - 1; i++) { str = "UPDATE criminal SET cr_id = " + editgrid.Rows[i].Cells[1].Value + ", cr_name= '" + editgrid.Rows[i].Cells[2].Value + "', cr_desc='" + editgrid.Rows[i].Cells[3].Value + "' ,cr_pic= '" + editgrid.Rows[i].Cells[4].Value + "', no_times ='" + editgrid.Rows[i].Cells[5].Value + "' WHERE cr_id = " + editgrid.Rows[i].Cells[1].Value; SqlCommand com = new SqlCommand(str, conn); com.ExecuteNonQuery(); label2.Visible = true; } it is not giving any errors but records are not updated,yet can anybody help me??
software student
Ok, first you are looping through the entire grid and updating every row even if it was not updated. Normally with a datagrid, you have an edit button which puts the row into edit mode. When the row is in edit mode you have a save and cancel button. Then with your save button you run the update for that single row. Now several post will probably follow talking about sql injection attacts. You should use a parameterize query or a stored procedure to do this update. Finally, my best guess to what your update problem is: I would guess this isn't the crid you think it is: editgrid.Rows[i].Cells[1].Value Hope that helps. Ben
-
i m editing records through datagrid..behind my update button i've written following code.. String str = " "; for (int i = 0; i == editgrid.RowCount - 1; i++) { str = "UPDATE criminal SET cr_id = " + editgrid.Rows[i].Cells[1].Value + ", cr_name= '" + editgrid.Rows[i].Cells[2].Value + "', cr_desc='" + editgrid.Rows[i].Cells[3].Value + "' ,cr_pic= '" + editgrid.Rows[i].Cells[4].Value + "', no_times ='" + editgrid.Rows[i].Cells[5].Value + "' WHERE cr_id = " + editgrid.Rows[i].Cells[1].Value; SqlCommand com = new SqlCommand(str, conn); com.ExecuteNonQuery(); label2.Visible = true; } it is not giving any errors but records are not updated,yet can anybody help me??
software student