【问题标题】:my update button only updates first checked row not others我的更新按钮只更新第一个选中的行而不是其他
【发布时间】:2011-02-01 14:42:14
【问题描述】:

我让数据库更新,但它只对选择的第一行更新,而不是为其他行更新。

     Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)

    For Each row As GridViewRow In GridView6.Rows
        ' Selects the text from the TextBox
        Dim selectedcheck As CheckBox = CType(row.FindControl("chkselect"), CheckBox)

        If selectedcheck.Checked = True Then
            Dim id As Label = CType(row.FindControl("id"), Label)
            cmd.Parameters.AddWithValue("@id", id.Text)
            cmd.Parameters.AddWithValue("@compby", txtagent.Text)
            cmd.Parameters.AddWithValue("@compdate", lbldate.Text)
            cmd.Parameters.AddWithValue("@comments", txtcomments.Text)


            cmd.CommandText = "dbo.updatetasks"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = conn

            conn.Open()

            cmd.BeginExecuteNonQuery()

            conn.Close()
        End If

    Next

End Sub

UPDATE dashboardtasks
       SET compby = @compby,
           comments = @comments,
           compdate = @compdate


       WHERE id = @id;

【问题讨论】:

  • BeginExecuteNonQuery 在命令执行完成之前返回。您当然不应该在之后立即关闭连接。如果您希望命令完成,请使用同步 ExecuteNonQuery。如果您想继续使用异步命令,则必须保持连接打开,捕获从BeginExecuteNonQuery 返回的 IAsyncResult,并稍后在EndExecuteNonQuery 中使用它
  • 可能需要存储过程代码来理解它在做什么。看来您正在通过键 id 更新一行。如果 id 是唯一的,那么它只会更新一条记录。看到您没有遍历一系列 id 字段,我会说它按预期工作。但是查看存储过程会有所帮助。
  • 我添加了我的存储过程代码,我正在循环遍历 ids 不是我吗?
  • 我以为我是因为 if 语句包含在 for each 语句中
  • 我希望您在第二次(以及后续)尝试执行时会遇到异常,因为您正在向同一个 cmd 对象添加越来越多的参数。您收到错误消息了吗?

标签: asp.net vb.net gridview sqldatasource


【解决方案1】:

我希望以下工作:

  Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)

    For Each row As GridViewRow In GridView6.Rows
        ' Selects the text from the TextBox
        Dim selectedcheck As CheckBox = CType(row.FindControl("chkselect"), CheckBox)

        If selectedcheck.Checked = True Then
            Dim id As Label = CType(row.FindControl("id"), Label)
            cmd.Parameters.Clear()     '<---- Stop adding more and more parameters
            cmd.Parameters.AddWithValue("@id", id.Text)
            cmd.Parameters.AddWithValue("@compby", txtagent.Text)
            cmd.Parameters.AddWithValue("@compdate", lbldate.Text)
            cmd.Parameters.AddWithValue("@comments", txtcomments.Text)


            cmd.CommandText = "dbo.updatetasks"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = conn

            conn.Open()

            cmd.ExecuteNonQuery() '<--- Don't use the asynchronous variant if you're not going to obey the contract

            conn.Close()
        End If

    Next

End Sub

如果您使用 ExecuteNonQuery 的同步变体,或者遵守异步方法的约定(您必须调用 EndExecuteNonQuery 来发现SQL 调用的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多