Gridview更新问题 - .NET

时间:2017-04-05 18:16:28

标签: c# mysql .net

我有gridview拉取我想要的数据但是我添加了一个按钮,当单击按钮时它将lastLeak更新为我设置的日期...我知道查询在SQL中工作所以不确定我缺少什么让它工作......

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="lastLeakCheck" Width="850px">
    <Columns>
        <asp:BoundField DataField="customerName" HeaderText="customer Name" SortExpression="customerName" />
        <asp:BoundField DataField="acctNum" HeaderText="acct Num" SortExpression="acctNum" />
        <asp:BoundField DataField="phoneNum" HeaderText="phone Num" SortExpression="phoneNum" />
        <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
        <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
        <asp:BoundField DataField="lastLeak" HeaderText="Last Leak" SortExpression="lastLeak" />
        <asp:ButtonField ButtonType="Button" CommandName="Update" HeaderText="Update Date" ShowHeader="True" Text="Completed"  />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="lastLeakCheck" runat="server" ConnectionString="Data Source=server;Initial Catalog=propane;User ID=user;Password=pass;Integrated Security=True" SelectCommand="SELECT customerName, acctNum, phoneNum, city, address, lastLeak from custInfo WHERE lastLeak &lt;= CONVERT(datetime, '4-6-2012' ) ORDER BY CONVERT(DATETIME, lastLeak) ASC"  UpdateCommand = "UPDATE custInfo SET lastLeak='4/5/2017'WHERE customerName='@customerName';"></asp:SqlDataSource>

这是我的按钮点击:

    cn = new SqlConnection(@"Data Source=server;Initial Catalog=propane;User ID=id;Password=pass;Integrated Security=True");


    cmd = new SqlCommand("UPDATE custInfo SET lastLeak='4/5/2017' WHERE customerName='@customerName'", cn);

    cn.Open();

    cmd.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

这是我创建网页和数据表并对其进行测试的完整解决方案。 Uploaded我可以在需要时下载我的网站

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Width="850px" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="customerName" HeaderText="customerName" SortExpression="customerName" />
        <asp:BoundField DataField="acctNum" HeaderText="acctNum" SortExpression="acctNum" />
        <asp:BoundField DataField="phoneNum" HeaderText="phoneNum" SortExpression="phoneNum" />
        <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
        <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
        <asp:BoundField DataField="lastLeak" HeaderText="lastLeak" SortExpression="lastLeak" />
        <asp:ButtonField ButtonType="Button" CommandName="UpdateLeak" HeaderText="Update Date" ShowHeader="True" Text="Completed"   />
    </Columns>
</asp:GridView>

页面加载

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

将泄漏绑定到网格

private void BindLeaks()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LeakConnection"].ConnectionString))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerName, AcctNum, PhoneNum, City, Address, LastLeak from Leak WHERE lastLeak >= CONVERT(datetime, '4-6-2012') ORDER BY LastLeak ASC", conn);
                DataSet dsLeaks = new DataSet();
                conn.Open();
                da.Fill(dsLeaks);
                conn.Close();
                GridView1.DataSource = dsLeaks;
                GridView1.DataBind();
            }

        }

用于更新数据的行命令事件

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateLeak")
            {
                if (e.CommandArgument != null)
                {
                    int RowID = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = GridView1.Rows[RowID];
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LeakConnection"].ConnectionString))
                    {
                        SqlCommand cmd = new SqlCommand("UPDATE Leak SET lastLeak='4/5/2017' WHERE CustomerName=@customerName", conn);
                        if (e.CommandArgument != null)
                            cmd.Parameters.AddWithValue("@customerName", row.Cells[0].Text.Trim());
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        BindLeaks();

                    }

                }
            }
        }