How To Store GridView Data In SQL Server Database Row By Row Basis

How To Store GridView Data In SQL Server Database Row By Row Basis

In this tutorial we will learn Gridview Rows Store In Database Rows or how to store gridview data in SQL Server database row by row basis.

Design View:

<div>
<table>
<asp:Panel ID="panelgrid" runat="server" BackColor="White" BorderStyle="None">
<tr><td colspan="5">
                 <asp:GridView ID="GridView1" runat="server" AllowPaging="true"
                 HeaderStyle-ForeColor="white" AutoGenerateColumns="false"
                 Font-Names="Arial" Font-Size="11pt" HeaderStyle-BackColor="green" Width="920px" EmptyDataText="No Recors Found For This Cretria" EmptyDataRowStyle-ForeColor="White" EmptyDataRowStyle-BorderColor="White" EditRowStyle-Font-Size="Large" EmptyDataRowStyle-BackColor="Gray">
                 <Columns>
                 <asp:TemplateField>
                 <HeaderTemplate>ID</HeaderTemplate>
                 <ItemTemplate><asp:Label ID="student_id" Enabled="false" runat="server" Text='<%#Eval("student_id") %>'>'></asp:Label></ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                 <HeaderTemplate>Attandance Status</HeaderTemplate>
                 <ItemTemplate> 
                 <asp:CheckBox ValidationGroup="vg" ID="CheckBoxa" runat="server" Text="P" /><asp:CheckBox ValidationGroup="vg" ID="CheckBoxp" runat="server" Text="A" /> </ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                 <HeaderTemplate>Class</HeaderTemplate>
                 <ItemTemplate><asp:TextBox ID="class_stu" Enabled="false" runat="server" BorderStyle="None" BackColor="White" Text='<%#Eval("class") %>'></asp:TextBox></ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                 <HeaderTemplate>Class</HeaderTemplate>
                 <ItemTemplate><asp:TextBox ID="section" Enabled="false" runat="server" BorderStyle="None" BackColor="White" Text='<%#Eval("section") %>'></asp:TextBox></ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                 <HeaderTemplate>Roll No</HeaderTemplate>
                 <ItemTemplate><asp:TextBox ID="student_roll" Enabled="false" runat="server" BorderStyle="None" BackColor="White" Text='<%#Eval("rollno") %>'></asp:TextBox></ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                 <HeaderTemplate>Student Name</HeaderTemplate>
                 <ItemTemplate><asp:TextBox ID="student_name" Enabled="false" BorderStyle="None" BackColor="White" runat="server" Text='<%#Eval("student name") %>'></asp:TextBox></ItemTemplate>
                 </asp:TemplateField>
                 
                 </Columns>
                 </asp:GridView>
                 </td></tr>
                 <tr><td align="right" colspan="5"> 
                 <asp:Button ID="Button1" runat="server" Text="Save Attandance" onclick="Button1_Click" /> </td></tr>
                 </asp:Panel>
                 </table>     
                 </div>

Code:

protected void Button1_Click(object sender, EventArgs e)
    {
        
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        String attandance_date = String.Empty;
        String attandance_status = String.Empty;
        String rollno = String.Empty;
        String student_name = String.Empty;
        String strsession = String.Empty;
        String class_stu = String.Empty;
        String section = String.Empty;

        using (SqlConnection scn = new SqlConnection(strcon))
        {
            foreach (GridViewRow GVRow in GridView1.Rows)
            {
                string dte = DateTime.Now.ToShortDateString();
                if (((CheckBox)GVRow.Cells[1].FindControl("CheckBoxa")).Checked == true)
                attandance_status = "p";
                else
                attandance_status = "a";
                class_stu = ((TextBox)GVRow.Cells[2].FindControl("class_stu")).Text;
                section = ((TextBox)GVRow.Cells[3].FindControl("section")).Text;
                rollno = ((TextBox)GVRow.Cells[4].FindControl("student_roll")).Text;
                student_name = ((TextBox)GVRow.Cells[5].FindControl("student_name")).Text;

                using (SqlCommand scm = scn.CreateCommand())
                {
                scn.Open();
                scm.CommandText = @"INSERT INTO attandance_data
                (attandance_date, attandance_status,class_stu,section, rollno, student_name)
                VALUES     ('" + TextBox1.Text + "','" + attandance_status.ToString() + "','" + class_stu.ToString()+ "','" + section.ToString() + "','" + rollno.ToString() + "','" + student_name.ToString() + "')";

                scm.ExecuteNonQuery();
                scn.Close();
                Response.Write("<script>alert('Attandance Has Been Marked')</script>");
                
                }
            }
        }

    }