title: How to get rows from Gridview and Insert them into database?
body: <p>I am developing a web application for a pharma-laboratory. </p>
  
  <p>While taking the order from the customer and clicking on the submit button, I have inserted the data into three tables of my database (customer, orderMaster, orderDetail). I have inserted into customer and orderMaster table. For orderDetail I have to insert data from the gridview.</p>
  
  <p>When I add a test against a customer, the test is added in the gridview. One gridview can have multiple tests against one customer.</p>
  
  <p>How can I insert data from gird view to database?</p>
  
  <p>My function for getting the data from the gridview and inserting it into the database is:</p>
  
  <pre><code>private void bindOrderDetails()
  {
      database_connectivity dbConnect = new database_connectivity();
      SqlConnection con = new SqlConnection();
      con = dbConnect.ConnectDB();
      con.Open();
      SqlCommand cmmd = new SqlCommand("SELECT MAX(orderID) as Order_Id FROM testOrder_master WHERE user_id = '" + Convert.ToInt32(txtUserID.Text) + "'", con);
      cmmd.Connection = con;
      SqlDataReader dr1 = cmmd.ExecuteReader();
      int orderID = 0;
      while (dr1.Read())
      {
          orderID = Convert.ToInt32(dr1["Order_Id"]);
      }
      con.Close();
      //for (int i = 0; i &lt; GridView1.Rows.Count - 1; i++)
      //{
      //    Stquery = @"INSERT INTO testOrder_details (orderID, labID, srNo, test_id, price, createdBy, creationTime) VALUES ('"+orderID+"','"+Convert.ToInt32(txtLabID.Text)+"','"+Convert.ToString(GridView1.Rows[i].Cells[1].
      //}
  
      foreach (GridViewRow row in GridView1.Rows)
      {
          con.Open();
          SqlCommand cmd1 = new SqlCommand("INSERT INTO testOrder_details (orderID, labID, srNo, test_id, price, createdBy, creationTime) VALUES (@orderId1,@lab_id,@serialNo,@testID, @testPrice, @created_by, @creation_time)", con);
          cmd1.Parameters.AddWithValue("@orderId1", orderID);
          cmd1.Parameters.AddWithValue("@lab_id", Convert.ToInt32(txtLabID.Text));
          cmd1.Parameters.AddWithValue("@serialNo", row.Cells[1].Text);
          cmd1.Parameters.AddWithValue("@testID", row.Cells[2].Text);
          cmd1.Parameters.AddWithValue("@testPrice", row.Cells[4].Text);
          cmd1.Parameters.AddWithValue("@created_by", Convert.ToString(txtUserTitle.Text));
          cmd1.Parameters.AddWithValue("@creation_time", DateTime.Now);
          cmd1.ExecuteNonQuery();
      }
  }
  </code></pre>
  
  <p>i have used break points and not getting any value for Serial No column. as i have used template field on auto increment for Serial No column.
  This is the asp code of my gridview:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      BackColor="White" BorderColor="#CCCCCC" 
      BorderStyle="None" BorderWidth="1px" CellPadding="3"
      onrowdeleting="GridView1_RowDeleting" 
      onselectedindexchanged="GridView1_SelectedIndexChanged"
      onrowcommand="GridView1_RowCommand" 
      onrowdatabound="GridView1_RowDataBound"&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowDeleteButton="True" ButtonType="Image" 
              DeleteImageUrl="~/Imagess/delete.jpg" /&gt;
          &lt;asp:TemplateField HeaderText="Serial No"&gt;
              &lt;ItemTemplate&gt;&lt;%# Container.DataItemIndex + 1 %&gt;
              &lt;/ItemTemplate&gt;
              &lt;ItemStyle Width="2%" /&gt; 
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:BoundField DataField="testID" HeaderText="Test ID" /&gt;
          &lt;asp:BoundField DataField="testName" HeaderText="Test Name" /&gt;
          &lt;asp:BoundField DataField="testPrice" HeaderText="Price" /&gt;
      &lt;/Columns&gt;
      &lt;FooterStyle BackColor="White" ForeColor="#000066" /&gt;
      &lt;HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /&gt;
      &lt;PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /&gt;
      &lt;RowStyle ForeColor="#000066" /&gt;
      &lt;SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#007DBB" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#00547E" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>