Akhter HUssain

Akhter HUssain

  • 1.1k
  • 1k
  • 45.9k

Insert rows into database from gridview

Apr 30 2020 8:12 AM

i am trying to insert records from gridview to database by using below code,but record is not getting insert,please have a look.

  1. protected void Button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             foreach (GridViewRow row in this.GridView1.Rows)  
  4.             {  
  5.                 if (row.RowType == DataControlRowType.DataRow)  
  6.                 {  
  7.                     // SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);  
  8.                     SqlCommand cmd = new SqlCommand("INSERT INTO tbl_Books VALUES(@B_Name,@B_IBN,@QTY)", con);  
  9.                     cmd.Parameters.AddWithValue("@B_Name", row.Cells[0].Text);  
  10.                     cmd.Parameters.AddWithValue("@B_IBN", row.Cells[1].Text);  
  11.                     cmd.Parameters.AddWithValue("@QTY", row.Cells[2].Text);  
  12.                     con.Open();  
  13.                     cmd.ExecuteScalar();  
  14.                     con.Close();  
  15.                 }  
  16.             }  
  17.         }  

Answers (1)