按钮单击Gridview以更新SQL值

时间:2017-04-07 13:18:24

标签: c# mysql asp.net gridview

我有一个gridview拉取值customerName,acctNum,phoneNum,city,address和lastLeak。数据全部拉得很好我拉起任何5岁或以上的最后一个漏洞。然后我在右边有一个按钮,一旦驾驶员进行检查,他就会点击该按钮,并且该行将lastLeak更新为今天的日期。出于某种原因,我不能让它工作我知道它必须接近才能让任何人发光。

这是我的gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="lastLeakCheck" Width="950px" 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="UpdateDate" 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=id;Password=pass;Integrated Security=True"
     SelectCommand="SELECT customerName, acctNum, phoneNum, city, address, lastLeak from custInfoWHERE lastLeak &lt;= CONVERT(datetime, '4-6-2012' )ORDER BY CONVERT(DATETIME, lastLeak) ASC" ></asp:SqlDataSource>

我的行的点击事件:

protected void GridView1_RowCommand(Object sender,GridViewCommandEventArgs e)     {

    if (e.CommandName == "UpdateDate")
    {

        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridView1.Rows[index];

        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/7/2017' WHERE customerName='@customerName'", cn);

        cmd.Parameters.AddWithValue("@customerName", row.Cells[0].Text);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        GridView1.DataBind();
    }

}

0 个答案:

没有答案