Hi Everyone,
I want to insert multiple rows in a table on clicking a button.
The process I used is going through each row of gridview and inserting the data into table. But if there is some problem in between the process then half rows get inserted and half rows don't. But it should be either all rows get inserted or none.
I cannot think of a way to achieve this. Can anyone help with any idea? I am using access 2013 with asp.net.
What I have tried:
For now, this is my code.
foreach (GridViewRow row in grid_score.Rows)
{
string revw_date = DateTime.Today.ToString();
string emp_fname = row.Cells[0].Text;
string emp_lname = row.Cells[1].Text;
string revwr_name = row.Cells[6].Text;
string intscore = row.Cells[8].Text;
sql = "INSERT INTO TABLENAME VALUES('" + revw_date + "',(SELECT EMP_ID FROM USER_DETAIL WHERE FIRST_NAME='" + emp_fname + "' AND LAST_NAME='" + emp_lname + "'),(SELECT REVIEWER_ID FROM USER_DETAIL WHERE REVIEWER_NAME='" + revwr_name + "')," + intscore + ")";
OleDbCommand cmd = new OleDbCommand(sql, conn);
int count = cmd.ExecuteNonQuery();
if (count == 1)
{
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('Data inserted successfully');", true);
}
else
{
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('Sorry! Data cannot be submitted. Try again.');", true);
}
}
As anyone can see from the code that success message will be displayed everytime a row gets inserted. This should not be the case.