Click here to Skip to main content
16,009,068 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
 protected void Button4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                con.Open();
                string ins = "insert into update (Regid,Rollno,Name,Course,Noofbooks,Bookid,Bookname) Values(@Regid,@Rollno,@Name,@Course,@Noofbooks,@Bookid,@Bookname)";
                SqlCommand com = new SqlCommand(ins, con);
                com.Parameters.AddWithValue("@Regid", GridView1.Rows[i].Cells[0].Text);
                com.Parameters.AddWithValue("@Rollno", GridView1.Rows[i].Cells[1].Text);
                com.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[2].Text);
                com.Parameters.AddWithValue("@Course", GridView1.Rows[i].Cells[3].Text);
                com.Parameters.AddWithValue("@Noofbooks", GridView1.Rows[i].Cells[4].Text);
                com.Parameters.AddWithValue("@Bookid", GridView1.Rows[i].Cells[5].Text);
                com.Parameters.AddWithValue("@Bookname", GridView1.Rows[i].Cells[6].Text);
                com.ExecuteNonQuery();
                con.Close();
            }
            Label1.Text = "Added successfully!";
        }
    }
}
Posted
Updated 17-May-15 3:18am
v2
Comments
Kornfeld Eliyahu Peter 17-May-15 9:28am    
Except the fact that you are opening and closing a connection for every iteration of the loop (waste of time/resources), what is your problem?
PreetamYadav 18-May-15 1:56am    
Show error



Incorrect syntax near the keyword 'update'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'update'.

Source Error:


Line 106: com.Parameters.AddWithValue("@Bookid", GridView1.Rows[i].Cells[5].Text);
Line 107: com.Parameters.AddWithValue("@Bookname", GridView1.Rows[i].Cells[6].Text);
Line 108: com.ExecuteNonQuery();
Line 109: con.Close();
Line 110: }

UPDATE is a reserved keyword in SQL and you can not name a table update!!!
NEVER USE RESERVED KEYWORD AS TABLE/COLUMN NAME!!!
 
Share this answer
 
your create table name as update, its a keyword, so use your table name like this [update] it wont show error

Follow the below code:

"
C#
string ins = "insert into [update] (Regid,Rollno,Name,Course,Noofbooks,Bookid,Bookname) Values(@Regid,@Rollno,@Name,@Course,@Noofbooks,@Bookid,@Bookname)";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900