You must Sign In to post a response.
  • Category: ASP.NET

    How to edit,update,delete,select data in sql server from gridview

    i have problem in Grid view (edit,update,delete,cancel). I have update my grid view data but not Back end not update help me. I need (edit,update,delete,cancel)
    Code .
  • #725502
    Hi,

    Refer below sample code

    protected void Bind_GV()
    {
    BLayer obj = new BLayer();
    DataSet ds = //data;
    GV.DataSource = ds;
    GV.DataBind();
    }
    protected void GV_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
    GV.EditIndex = e.NewEditIndex;
    Bind_GV();
    }
    protected void GV_RowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GV.EditIndex = -1;
    Bind_GV();
    }
    protected void GV_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    BLayer obj = new BLayer();
    GridViewRow row = (GridViewRow)GV.Rows[e.RowIndex];
    string Mat_Id = ((Label)row.FindControl("lblMat_Id")).Text;
    string Grade_Id = //fetch data
    string Item_Id = //fetch data
    Result = Delete_Data( Mat_Id, Grade_Id, Item_Id, UserId.ToString());
    Bind_GV();
    }
    protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    BLayer obj = new BLayer();
    Label Mat_Id = (Label)GV.Rows[e.RowIndex].FindControl("lblMat_Id");
    Label Grade_Id = //fetch data
    Label Item_Id = //fetch data
    TextBox qty = (TextBox)GV.Rows[e.RowIndex].FindControl("txtEditQty");
    Result = obj.Update_Details(Mat_Id.Text, Grade_Id.Text, Item_Id.Text, qty.Text, UserId.ToString());
    GV.EditIndex = -1;
    Bind_Tender_Quantity_GV();
    }


    This is sample code for Displaying, Edit,cancelling, Update, Delete. For more information refer below link

    http://www.dotnetspider.com/resources/44926-GridView-Edit-Cancel-Update-Delete-ASP.net.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #725511
    Use below code sample to edit update data through grid view

    Table structure

    create table emp(eno int,empname varchar(50),sal bigint)


    Client side

    <asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server"/><br />
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="false"
    onrowediting="GridView1_RowEditing"
    onrowcancelingedit="GridView1_RowCancelingEdit"
    onrowdeleting="GridView1_RowDeleting"
    onrowupdating="GridView1_RowUpdating"
    onpageindexchanging="GridView1_PageIndexChanging" PageSize="5" AllowPaging="true">
    <Columns>
    <asp:TemplateField HeaderText="Employee no">
    <ItemTemplate>
    <%#Eval("eno")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Emp name">
    <ItemTemplate>
    <%#Eval("empname")%>

    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtempname" runat="server" Text='<%#Bind("empname")%>'></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Employee Name cannot be blank!" ControlToValidate="txtempname" Display="None" ></asp:RequiredFieldValidator>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Salary">
    <ItemTemplate>
    <%#Eval("sal")%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtsal" runat="server" Text='<%#Bind("sal")%>'></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Employee Salary cannot be blank!" ControlToValidate="txtsal" Display="None"></asp:RequiredFieldValidator>
    </EditItemTemplate>

    </asp:TemplateField>
    <asp:CommandField HeaderText="Modify" ShowEditButton="true" EditText="Edit">
    <ControlStyle Width="50" />
    </asp:CommandField>
    <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?');"
    >Delete</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </ContentTemplate>
    </asp:UpdatePanel>


    server side

    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    SqlCommand sqlcmd;
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();

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

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

    void GridData()
    {
    sqlcmd = new SqlCommand("select * from emp", sqlcon);
    sqlcon.Open();
    da = new SqlDataAdapter(sqlcmd);
    dt.Clear();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    //Align Width if grid view column
    GridView1.Columns[1].ItemStyle.Width = 100;

    //Align Grid view Name coloumn as left
    GridView1.Columns[1].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    GridData();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    String eno;
    eno = GridView1.DataKeys[e.RowIndex].Value.ToString();
    sqlcmd = new SqlCommand("delete from emp where eno='" + eno +"'", sqlcon);
    sqlcon.Open();
    sqlcmd.CommandType = CommandType.Text;
    sqlcmd.ExecuteNonQuery();
    sqlcon.Close();
    GridData();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    GridViewRow row = GridView1.Rows[e.RowIndex];
    string eno;

    eno = GridView1.DataKeys[e.RowIndex].Value.ToString();

    TextBox empname = (TextBox)row.FindControl("txtempname");
    TextBox sal = (TextBox)row.FindControl("txtsal");

    sqlcon.Open();
    sqlcmd = new SqlCommand("update emp set empname='" + empname.Text +"',sal='" + sal.Text +"' where eno='" + eno + "'" , sqlcon);
    sqlcmd.CommandType = CommandType.Text;
    sqlcmd.ExecuteNonQuery();
    sqlcon.Close();
    GridView1.EditIndex = -1;
    GridData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    GridView1.PageIndex = e.NewPageIndex;
    GridData();
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725522
    [Response removed by Admin. Read forum policies.]
    Regards
    Rakesh Chaubey
    rakeshchaubey1989@gmail.com


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.