title: How to save gridview each row value in table?
body: <p>How to insert gridview each row value into database.I use below code but <code>null</code> values are pass in strings.</p>
  
  <p><img src="https://i.stack.imgur.com/2Veie.png" alt=""></p>
  
  <pre><code>foreach (GridViewRow gvrow in GridView1.Rows)
              {
                  con.Open();
  
                  string datetime = Request.Form["txtdate"];
                  str += GridView1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
                  Id  += gvrow.Cells[0].Text;
                  fName += gvrow.Cells[1].Text;
                  FaName  += gvrow.Cells[2].Text;
                  LName  += gvrow.Cells[3].Text;
                  attendance  += gvrow.Cells[4].Text;
                  remarks  += gvrow.Cells[5].Text;
                  SqlCommand cmd = new SqlCommand("INSERT INTO [first].[dbo].[Staff_Attendance]([Id],[Department],[Date],[First_name],[Father_name],[Last_name],[Attendance],[Remarks]) VALUES(@Id, @Department, @Date, @First_name, @Father_name, @Last_name, @Attendance, @Remarks)", con);
                  cmd.Parameters.AddWithValue("@Id", Id);
                  cmd.Parameters.AddWithValue("@Department", DropDownList1.SelectedItem .ToString ());
                  cmd.Parameters.AddWithValue("@Date", datetime.ToString());
  
                  cmd.Parameters.AddWithValue("@First_name", fName);
                  cmd.Parameters.AddWithValue("@Father_name", FaName);
                  cmd.Parameters.AddWithValue("@Last_name", LName);
                  cmd.Parameters.AddWithValue("@Attendance", attendance);
                  cmd.Parameters.AddWithValue("@Remarks", remarks);
  
                  cmd.ExecuteNonQuery();
                  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Data Have Been Saved')", true);
                  con.Close();
              }
  </code></pre>
  
  <p>Gridview code:-</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" 
                      PageSize="5" AutoGenerateColumns="False" DataKeyNames ="srno"&gt;
                      &lt;PagerSettings FirstPageText="First" LastPageText="Last" 
                          Mode="NumericFirstLast" PageButtonCount="5" /&gt;
                          &lt;Columns &gt;
                          &lt;asp:TemplateField HeaderText="Sr.No."&gt;
  
  
      &lt;ItemTemplate&gt;
    &lt;asp:Label ID="lblsrno" runat="server" Text='&lt;%#Eval("srno") %&gt;'&gt;
      &lt;/asp:Label&gt;
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
      &lt;asp:TemplateField HeaderText="First Name"&gt;
  
  
      &lt;ItemTemplate&gt;
    &lt;asp:Label ID="lblFname" runat="server" Text='&lt;%#Eval("first_name") %&gt;'&gt;
      &lt;/asp:Label&gt;
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
         &lt;asp:TemplateField HeaderText="Father Name"&gt;
  
  
      &lt;ItemTemplate&gt;
    &lt;asp:Label ID="lblFaName" runat="server" Text='&lt;%#Eval("father_name") %&gt;'&gt;
      &lt;/asp:Label&gt;
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
         &lt;asp:TemplateField HeaderText="Last Name"&gt;
  
  
      &lt;ItemTemplate&gt;
    &lt;asp:Label ID="lblLName" runat="server" Text='&lt;%#Eval("last_name") %&gt;'&gt;
      &lt;/asp:Label&gt;
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
         &lt;asp:TemplateField HeaderText="Attendance"&gt;
  
  
      &lt;ItemTemplate&gt;
   &lt;asp:DropDownList ID="ddlDesignation" runat="server" Width ="80px"&gt;
  &lt;asp:ListItem Text ="--Select--" &gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text ="P"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text ="A"&gt;&lt;/asp:ListItem&gt;
                          &lt;/asp:DropDownList&gt;
  
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
  
      &lt;asp:TemplateField HeaderText="Remarks"&gt;
  
  
      &lt;ItemTemplate&gt;
   &lt;asp:DropDownList ID="ddlRemark" runat="server" Width ="80px"&gt;
  &lt;asp:ListItem Text ="--Select--" &gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text ="Paid Leave"&gt;&lt;/asp:ListItem&gt;
  &lt;asp:ListItem Text ="Unpaid Leave"&gt;&lt;/asp:ListItem&gt;
                          &lt;/asp:DropDownList&gt;
  
  
      &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
                          &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>In database it stored null values</p>
  
  <p><img src="https://i.stack.imgur.com/5ZZOb.png" alt="enter image description here"></p>