一键添加空白记录 [英] one click add blank record

查看:119
本文介绍了一键添加空白记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,
当单击编辑链接"按钮时,网格中又添加了一个空白记录,请纠正我.

代码是:

Dear sir,
when was clicking edit link button there is one more blank record added to the grid please correct me.

The code is:

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();
    }
}

推荐答案

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


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


This Link Button Have CommandName = "Edit" 





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();

    }


这篇关于一键添加空白记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆