Friday, May 11, 2012

Insert Multiple Records into Database from GridView


     This is a common concept,  which we use in our project maximum times. User stores records into gridview one after another and save all records from gridview into database at a single push.

Before starting the project we have to design the table according to our requirement

Table  Design


Column                  DataType
StudentRoll(PK)   varchar(20)
StudentAge int
StudentAddress varchar(50)


Source Code (Design View)


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Multiple datainsert in database by gridview.aspx.cs" Inherits="Multiple_datainsert_in_database_by_gridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form2" runat="server">
    <div>
 
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td style="text-align: right">
    <asp:Label ID="Label1" runat="server" Text="Roll"></asp:Label>
                </td>
                <td>
     
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td width="40%" style="text-align: right">
    <asp:Label ID="Label2" runat="server" Text="Name" ></asp:Label>
                </td>
                <td width="60%">

 
        <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
    <asp:Label ID="Label3" runat="server" Text="Address"></asp:Label>
 
                </td>
                <td>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                  <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click"/>
    <asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
    <asp:Button ID="Button3" runat="server" Text="Clear" onclick="Button3_Click" />

                    &nbsp;</td>
            </tr>
        </table>

        &nbsp;

    <asp:GridView ID="GridView1" runat="server" >
    </asp:GridView>
    </div>
 
    </form>
</body>
</html>

 C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Multiple_datainsert_in_database_by_gridview : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\\Database.mdf;User Instance=true");
    SqlCommand cmd;
    static int flag = 0;
    static int rowno = 0;
    DataTable dt;

    # region Method
    protected void fillGrid()
    {
        dt = new DataTable();
        dt = (DataTable)ViewState["Datatable"];
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        else
        {
            GridView1.Dispose();
            GridView1.DataBind();
        }

    }
    public DataTable Createtable()
    {
        dt = new DataTable();
        dt.Columns.Add("Roll", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Address", typeof(string));
        ViewState["Datatable"] = dt;
        return dt;
    }
    # endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Createtable();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        dt = new DataTable();
        dt = (DataTable)ViewState["Datatable"];
        DataRow dr;        
        dr = dt.NewRow();
        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;
        dr[2] = TextBox3.Text;
        dt.Rows.Add(dr);    
      
        ViewState["Datatable"] = dt;
        fillGrid();
    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow r in GridView1.Rows)
        {
            int a = int.Parse(r.Cells[0].Text.Trim());
            string b = r.Cells[1].Text.Trim();
            string c = r.Cells[2].Text.Trim();
            SqlCommand cmd = new SqlCommand("Insert into Student values('" + a + "','" + b + "','" + c + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        ViewState["Datatable"] = null;       
        GridView1.DataSource = null;
        GridView1.DataBind();     
        Createtable();
    }
}

2 comments:

  1. Nice post, thanks for sharing this useful information.Founded in 1989, Texus is a US-headquartered provider of custom software development and IT consulting services with 450+ IT professionals located internationally.
    More Details Visit Our Website:
    Texus
    Texus Systems

    ReplyDelete