Gridview insert update delete

.........................default.aspx.........................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewInsertUpdateDelete.aspx.cs" Inherits="GridViewInsertUpdateDelete" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Salary" HeaderText="Salary"
                    SortExpression="Salary" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>
...............................end........................
,,,,,,,,,,,,,,,........................default.aspx.cs......................................
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.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class GridViewInsertUpdateDelete : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlCommand cmd;
    SqlDataAdapter da;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }
    protected void bindData()
    {
        string str = "select*from emp";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string str=GridView1.DataKeys[e.RowIndex].Value.ToString();
        TextBox name=(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox salary=(TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        string strUpdate = "update emp set name='"+name.Text.ToString()+"',salary='"+salary.Text.ToString()+"' where id='"+str+"'";
        cmd = new SqlCommand(strUpdate,con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        bindData();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindData();

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

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindData();
    }
}
...............................end......................................
Share:

No comments:

Post a Comment

Wednesday, 14 May 2014

Gridview insert update delete

.........................default.aspx.........................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewInsertUpdateDelete.aspx.cs" Inherits="GridViewInsertUpdateDelete" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Salary" HeaderText="Salary"
                    SortExpression="Salary" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>
...............................end........................
,,,,,,,,,,,,,,,........................default.aspx.cs......................................
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.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class GridViewInsertUpdateDelete : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlCommand cmd;
    SqlDataAdapter da;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }
    protected void bindData()
    {
        string str = "select*from emp";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string str=GridView1.DataKeys[e.RowIndex].Value.ToString();
        TextBox name=(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox salary=(TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        string strUpdate = "update emp set name='"+name.Text.ToString()+"',salary='"+salary.Text.ToString()+"' where id='"+str+"'";
        cmd = new SqlCommand(strUpdate,con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        bindData();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindData();

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

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindData();
    }
}
...............................end......................................

No comments:

Post a Comment

Popular

Total Pageviews

Archive