更新数据时无法检测gridview中的行

时间:2014-07-04 09:05:34

标签: asp.net vb.net gridview

我想通过点击批准或拒绝按钮来更新AResult。

在我的aspx页面中,这里是我的代码,

<asp:ButtonField ButtonType="Button" CommandName="Approve" Text="Approve" runat="server" />
<asp:ButtonField ButtonType="Button" CommandName="Reject" Text="Reject" runat="server" />

我的aspx.vb代码,

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand

    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)


    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
    con.Open()




    If e.CommandName.CompareTo("Approve") = 0 OrElse _
       e.CommandName.CompareTo("Reject") = 0 Then
        ' The Approve or Reject Button has been clicked
        ' Determine the ID of the appointment whose result was adjusted

        Dim AID As Integer = Convert.ToInt32(_GridView1.DataKeys(Convert.ToInt32(e.CommandArgument)).Value)


        Dim cmd As New SqlCommand

        cmd.Connection = con
        If e.CommandName.CompareTo("Approve") = 0 Then
            cmd = New SqlCommand("UPDATE [Appointment] SET AResulT  = N'Approve'", con)


        ElseIf e.CommandName.CompareTo("Reject") = 0 Then
            cmd = New SqlCommand("UPDATE [Appointment] SET AResult = N'Reject'", con)


        End If

        cmd.ExecuteNonQuery()

        Response.Redirect("Waiting List")

        con.Close()


    End If
End Sub

当我点击批准按钮时,它将通过批准更新我的整个约会结果。当我点击按钮时,有人可以教我如何检测行吗?

1 个答案:

答案 0 :(得分:1)

在更新语句中,您不只更新一行,而是更新整个表

  cmd = New SqlCommand("UPDATE [Appointment] SET AResulT  = N'Approve'", con)

您需要将此查询更改为

  cmd = New SqlCommand("UPDATE [Appointment] SET AResulT  = N'Approve' WHERE [KeyColumnName]=" & AID, con)