错误 - '行'不是'字符串'的成员不确定为什么?

时间:2014-05-19 00:27:29

标签: vb.net

我试图在网格视图中设置一个复选框来设置1或0(真/假)并且我在后面的代码中出现错误

行不是字符串

的成员

此链接:

 Dim ID As String = [ID].Rows(dgRow.DataItemIndex).Cells(0).Text

我不知道为什么,我是这个东西的完全新手,请放轻松,这是我背后的代码。

Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)       'Handles checkbox.CheckedChanged
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("BusinessSignUpsConnectionString").ConnectionString


    Dim box As CheckBox = DirectCast(sender, CheckBox)
    Dim tblcell As TableCell = CType(box.Parent, TableCell)
    Dim dgRow As GridViewRow = CType(tblcell.Parent, GridViewRow)

    Dim ID As String = [ID].Rows(dgRow.DataItemIndex).Cells(0).Text


    Dim insertSQL As String

    If box.Checked = True Then
        insertSQL = "UPDATE BusinessSignups "
        insertSQL &= "SET contacted=1 "
        insertSQL &= "WHERE [ID]= @ID "
    Else
        insertSQL = "UPDATE BusinessSignups "
        insertSQL &= "SET contacted=0 "
        insertSQL &= "WHERE [ID]= @ID "
    End If

    Using con As New SqlConnection(connectionString)
        Dim cmd As New SqlCommand(insertSQL, con)
        cmd.Parameters.AddWithValue("@contacted", contacted)
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch Err As SqlException
            MsgBox("Error", 65584, "Insertion Error")
        End Try
        con.Close()
    End Using

End Sub

1 个答案:

答案 0 :(得分:0)

如果您的网格名为ID,那么您不能对变量使用相同的名称,它将从方法内的类范围中隐藏变量。为变量使用不同的名称:

Dim identity As String = [ID].Rows(dgRow.DataItemIndex).Cells(0).Text

通过该错误,接下来是您使用不存在的变量设置错误的参数。您希望将名称"@ID"与上面的变量一起使用:

cmd.Parameters.AddWithValue("@ID", identity)