我的更新按钮仅更新第一个选中的行而不更新其他行

发布于 2024-10-15 04:42:34 字数 1204 浏览 4 评论 0原文

我要更新数据库,但它只对选定的第一行进行更新,而不对其他行进行更新。

     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;

I got the database to update, but it only does it for the first row selected not for the others.

     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

enter image description here

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


       WHERE id = @id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-10-22 04:42:34

我希望以下内容能够工作:

  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 调用的结果。

I'd expect the following to work:

  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

You would have gotten a nice exception telling you what you'd done wrong if you'd used the synchronous variant of ExecuteNonQuery, or obeyed the contract for Asynchronous methods (where you have to call EndExecuteNonQuery to discover the result of your SQL call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文