Tuesday 21 May 2013

Gridview Insert with Footer control:


Gridview Insert with Footer control:
.ASPX Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridview Insert.aspx.cs" Inherits="Gridview_Gridview_Insert" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="Eid" 
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting"
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
            AutoGenerateEditButton="True">
            <Columns>
                <asp:TemplateField HeaderText="Eid" InsertVisible="False" SortExpression="Eid">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Eid") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Eid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Ename" SortExpression="Ename">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Ename") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Ename") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox10" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PresentAdress" SortExpression="PresentAdress">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("PresentAdress") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("PresentAdress") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Area" SortExpression="Area">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Area") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>MANINAGAR</asp:ListItem>
                            <asp:ListItem>CGROAD</asp:ListItem>
                            <asp:ListItem>ASHRAM ROAD</asp:ListItem>
                            <asp:ListItem>LAL DARWAJA</asp:ListItem>
                            <asp:ListItem>CTM</asp:ListItem>
                            <asp:ListItem>KANKARIA</asp:ListItem>
                            <asp:ListItem>NAVRANGPURA</asp:ListItem>
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Area") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City" SortExpression="City">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server">
                            <asp:ListItem>Ahmedabad</asp:ListItem>
                            <asp:ListItem>Baroda</asp:ListItem>
                            <asp:ListItem>Surat</asp:ListItem>
                            <asp:ListItem>Janmnagar</asp:ListItem>
                            <asp:ListItem>Junaghad</asp:ListItem>
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country" SortExpression="Country">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server">
                            <asp:ListItem>India</asp:ListItem>
                            <asp:ListItem>Usa</asp:ListItem>
                            <asp:ListItem>Australia</asp:ListItem>
                            <asp:ListItem>Canada</asp:ListItem>
                            <asp:ListItem>Uk</asp:ListItem>
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Qualification" SortExpression="Qualification">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Qualification") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem>10th</asp:ListItem>
                            <asp:ListItem>12th</asp:ListItem>
                            <asp:ListItem>Bcom</asp:ListItem>
                            <asp:ListItem>Ba</asp:ListItem>
                            <asp:ListItem>Bsc</asp:ListItem>
                            <asp:ListItem>Bca</asp:ListItem>
                            <asp:ListItem>BE</asp:ListItem>
                            <asp:ListItem>Mcom</asp:ListItem>
                          
                            <asp:ListItem>Ma</asp:ListItem>
                            <asp:ListItem>Msc</asp:ListItem>
                            <asp:ListItem>Me</asp:ListItem>
                            <asp:ListItem>Mca</asp:ListItem>
                            <asp:ListItem>Other</asp:ListItem>
                        </asp:CheckBoxList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("Qualification") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email" SortExpression="Email">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
                        <asp:Button ID="Button2" runat="server" Text="Insert" CommandName="Insert" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
           
           
           
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="ADDNEW RECORDS"
            onclick="Button1_Click" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [Employee]" >
           
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

.CS Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Gridview_Gridview_Insert : System.Web.UI.Page
{
    string cnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        } 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        binddata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binddata();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtNewFirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox txtNewLastName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox txtNewMobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        TextBox txtNewArea = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        TextBox txtNewCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
        TextBox txtNewState = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
        TextBox txtNewEdu = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
        TextBox txtNewEmail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");

        Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), txtNewFirstName.Text, txtNewLastName.Text, txtNewMobile.Text, txtNewArea.Text, txtNewCity.Text, txtNewState.Text, txtNewEdu.Text, txtNewEmail.Text);
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewFirstName = (TextBox)GridView1.FooterRow.FindControl("TextBox9");
            TextBox txtNewLastName = (TextBox)GridView1.FooterRow.FindControl("TextBox10");
            TextBox txtNewMobile = (TextBox)GridView1.FooterRow.FindControl("TextBox11");
            DropDownList ddlAre = (DropDownList)GridView1.FooterRow.FindControl("DropDownList1");
            DropDownList ddlcity = (DropDownList)GridView1.FooterRow.FindControl("DropDownList2");
            DropDownList ddlstate = (DropDownList)GridView1.FooterRow.FindControl("DropDownList3");
            CheckBoxList chkedu = (CheckBoxList)GridView1.FooterRow.FindControl("CheckBoxList1");
            TextBox txtemail = (TextBox)GridView1.FooterRow.FindControl("TextBox12");
            Insert(txtNewFirstName.Text, txtNewLastName.Text, txtNewMobile.Text, ddlAre.SelectedValue, ddlcity.SelectedValue, ddlstate.SelectedValue, chkedu.SelectedValue, txtemail.Text);
        }
        //GridView1.DataBind();
        binddata();


    }
    public DataTable Fetch()
    {
        string sql = "Select * From Employee";
        SqlDataAdapter da = new SqlDataAdapter(sql, cnstr);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
        // Write your own Logic for fetching data, this method should return a DataTable
    }

    public void Insert(string Ename, string Mobile, string PresentAdress, string Area, string City, string Country, string Qualification, string Email)
    {
        //string sql = "INSERT INTO UserTable ([First Name],[Last Name],[Login Id],[Password],[Start Date],[End Date],[Email Address]) ";
        //sql += " VALUES ('" + FirstName + "','" + LastName + "','" + LoginId + "','" + Password + "','" + StartDate + "','" + EndDate + "','" + EmailAddress + "')";

        string sql = "INSERT INTO [Employee] ([Ename], [Mobile], [PresentAdress], [Area], [City], [Country], [Qualification], [Email])";
        sql += "VALUES ('" + Ename + " ', '" + Mobile + "', '" + PresentAdress + "' , '" + Area + "', '" + City + "', '" + Country + "', '" + Qualification + "', '" + Email + "')";
        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }
    public void Update(int Eid, string Ename, string Mobile, string PresentAdress, string Area, string City, string Country, string Qualification, string Email)
    {


        // string sql = "UPDATE UserTable SET [First Name] = '" + FirstName + "',[Last Name] = '" + LastName + "',[Login Id] = '" + LoginId + "' ,[Password] = '" + Password

        //+ "'";
        // sql += ",[Start Date] = '" + StartDate + "',[End Date] = '" + EndDate + "',[Email Address] = '" + EmailAddress + "' WHERE Id=" + id;


        string sql = "UPDATE Employee SET [Ename] = '" + Ename + " ', [Mobile] = ' " + Mobile + "' , [PresentAdress] =' " + PresentAdress + "' , [Area] = ' " + Area + "', [City] = ' " + City + "', [Country] = ' " + Country + "', [Qualification] = ' " + Qualification + "', [Email] =' " + Email + "'  WHERE Eid = " + Eid;


        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();

    }
    public void Delete(int Eid)
    {
        string sql = "DELETE FROM Employee WHERE Eid=" + Eid;
        SqlConnection conn = new SqlConnection(cnstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
        // Write your own Delete statement blocks.
    }

    void binddata()
    {
       
      
       SqlDataAdapter  da = new SqlDataAdapter("Select * from Employee", cnstr);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }
}


No comments:

Post a Comment