Thursday, 5 December 2013

Gridview Operation of asp.net

How to Insert,Update and Delete in Asp.net:-

First create a table in your sqlserver database like this:-
Suppose this table name is rabi:-
eid
Int(set primary key) set Is Identity=true
ename
varchar(50)
ads
varchar(50)



Now Design Gridview  like this  :-
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="eid"
            CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="eid">
                <ItemTemplate>
                <asp:Label ID="lbleid" runat="server" Text='<%#Eval("eid") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ename">
                    <EditItemTemplate>
                        <asp:TextBox ID="T1" runat="server" Text='<%#Eval("ename") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtename" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("ename") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ads">
                    <EditItemTemplate>
                        <asp:TextBox ID="T2" runat="server" Text='<%#Eval("ads")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtads" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("ads") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit">
                    <FooterTemplate>
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
                            CommandName="Edit" Text="Edit"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Update">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false"
                            CommandName="Update" Text="Update"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false"
                            CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="false"
                            CommandName="Select" Text="Select"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <br />
        <asp:Label ID="lblmsg" runat="server"></asp:Label>
        <br />
   
    </div>
    </form>
</body>


Now write this Code in the code behind page for Gridview Operation:-

Import this at top of the page:-
using System.Data;
using System.Data.SqlClient;


write your Querystring at public partial class

SqlConnection con = new SqlConnection("Data Source=xyz;Initial Catalog=x;Integrated Security=True");



After write the query string Write this code for Gridview operation:-

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDetails();
        }
    }

    protected void BindDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from rabi", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
      }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ename=((TextBox)GridView1.FooterRow.FindControl("txtename")).Text;
        string ads=((TextBox)GridView1.FooterRow.FindControl("txtads")).Text;
        string sql = "INSERT INTO rabi(ename, ads) VALUES ('" + ename + "','" + ads + "')";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        con.Close();
        BindDetails();
        lblmsg.Text = "sucessfully.........";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ename = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
        string ads = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text;
        string sql = "delete from rabi where ename= '"+ename+"'";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        BindDetails();
        lblmsg.Text = "Ur record deleted sucessfully....thanks...";
       
       
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindDetails();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindDetails();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  
 {
        try
        {
            int eid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox ename = (TextBox)GridView1.Rows[e.RowIndex].FindControl("T1");
            TextBox ads = (TextBox)GridView1.Rows[e.RowIndex].FindControl("T2");

            con.Open();
            SqlCommand cmd = new SqlCommand("update rabi set ename='" + ename.Text +                "',ads='" + ads.Text + "' where eid=" + eid, con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = " Details Updated successfully";
            GridView1.EditIndex = -1;
            BindDetails();
        }
        catch
        {
            lblmsg.Text = "sorry i can't update";
        }
    }

This is insert,update and delete operation on Gridview:-
Thanks of all of my friends…


No comments:

Post a Comment