Asp.net “我的更新”按钮仅更新第一个选中的行,而不更新其他行

Asp.net “我的更新”按钮仅更新第一个选中的行,而不更新其他行,asp.net,vb.net,gridview,sqldatasource,Asp.net,Vb.net,Gridview,Sqldatasource,我得到了要更新的数据库,但它只对选择的第一行进行更新,而不对其他行进行更新 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 CheckBo

我得到了要更新的数据库,但它只对选择的第一行进行更新,而不对其他行进行更新

     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


我希望以下方法能够奏效:

  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
Protected Sub btnUpdate\u单击(ByVal发送者作为对象,ByVal e作为事件参数)
作为GridView6.Rows中的GridViewRow的每一行
'从文本框中选择文本
Dim selectedcheck As CheckBox=CType(row.FindControl(“chkselect”),复选框)
如果选择Check.Checked=True,则
作为标签的Dim id=CType(row.FindControl(“id”)、标签)

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