c# - Gridview 更新问题 - .NET

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

最佳答案

这是我创建网页和数据表并对其进行测试的完整解决方案。 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();
            }
        }

将泄漏绑定(bind)到网格

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

                    }

                }
            }
        }

关于c# - Gridview 更新问题 - .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238735/

相关文章:

c# - 如何检查 MemoryCache 中是否存在键

c# - 从 ControlTemplate 绑定(bind)到 ViewModel

c# - 列数据不能为空 c# mysql

mysql - FASTest > mysql 获取最后的不同记录返回所有列

数组的 C# 互操作编码(marshal)行为似乎与文档不一致

c# - 按字符串创建类的实例,但要考虑隐式转换 - C# 反射

c# - 具有多个存储库和服务的 .NET MVC Controller ?

c# - 本地 NuGet 包源不显示包

c# - 如何获取 DataGridView 行值并将其存储在变量中?

mysql - 如何在 Opencart 数据库中的表之间进行 SELECT 连接?