Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,
when was clicking edit link button there is one more blank record added to the grid please correct me.

The code is:
C#
private void Binddata()
    {
        string selectSQL = "SELECT * FROM reservation";
        ocon.Open();
        SqlCommand cmd = new SqlCommand(selectSQL, ocon);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        adapter.Fill(ds, "reservation");
        ocon.Close();
        gvreservation.DataSource = ds;
        gvreservation.DataBind();
    }

    protected void gvreservation_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
        string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
        string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
        string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
        string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
        ocon.Open();
        string sqlstatement ="Insert into reservation(CustomerID,CustomerName,Age,Location,Class)values(@CustomerID,@CustomerName,@Age,@Location,@Class)";
        SqlCommand cmd = new SqlCommand(sqlstatement,ocon);
        cmd.Parameters.Add("@CustomerID",id);
        cmd.Parameters.Add("@CustomerName",name);
        cmd.Parameters.Add("@Age",age);
        cmd.Parameters.Add("@Location",location);
        cmd.Parameters.Add("@Class",class1);
        cmd.ExecuteNonQuery();
        ocon.Close();
        Binddata();
    }

    protected void gvreservation_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
        string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
        string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
        string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
        string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
        string sqlstatements = "update reservation set CustomerName='"+name+"',Age='"+age+"',Location='"+location+"',Class='"+class1+"' where CustomerID='"+id+"'";
        ocon.Open();
        SqlCommand cmd = new SqlCommand(sqlstatements,ocon);
        cmd.Parameters.Add("@CustomerID",id);
        cmd.Parameters.Add("@CustomerName",name);
        cmd.Parameters.Add("@Age",age);
        cmd.Parameters.Add("@Location",location);
        cmd.Parameters.Add("@Class",class1);
        cmd.ExecuteNonQuery();
        ocon.Close();
        Binddata();
    }

    protected void gvreservation_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
        string sqlstatement = "Delete from reservation where CustomerID=@CustomerID";
        ocon.Open();
        SqlCommand cmd = new SqlCommand(sqlstatement,ocon);
        cmd.Parameters.Add("@CustomerID",id);
        cmd.ExecuteNonQuery();
        ocon.Close();
        Binddata();
    }
}
Posted
Updated 26-Sep-11 2:21am
v2
Comments
Ankur\m/ 26-Sep-11 8:21am    
Do format your code when you are pasting code snippet. It increases the readability and chances of getting answer.

1 solution

First You need to give the command name to the edit link button For Example.

C#
<asp:linkbutton id="lnkEdit" runat="server" commandname="Edit" commandargument="<%# Container.DataItemIndex %>" xmlns:asp="#unknown">

This Link Button Have CommandName = "Edit" 


C#
protected void gvreservation_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
        string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
        string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
        string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
        string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
        string sqlstatements = "update reservation set CustomerName='"+name+"',Age='"+age+"',Location='"+location+"',Class='"+class1+"' where CustomerID='"+id+"'";
        ocon.Open();
        SqlCommand cmd = new SqlCommand(sqlstatements,ocon);
        cmd.Parameters.Add("@CustomerID",id);
        cmd.Parameters.Add("@CustomerName",name);
        cmd.Parameters.Add("@Age",age);
        cmd.Parameters.Add("@Location",location);
        cmd.Parameters.Add("@Class",class1);
        cmd.ExecuteNonQuery();
        ocon.Close();
        gvreservation.EditIndex = e.NewEditIndex;
        Binddata();

    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900