vb 2010 InvalidCastException错误

时间:2013-10-07 15:15:43

标签: vb.net visual-studio-2010

我正在尝试创建一个简单的访问数据库登录表单,并在运行时收到上述错误。仅当用户组合正确时才会发生这种情况。如果登录不正确,则显示无效消息。但是,如果我提供正确的凭据,则会抛出此错误。有人可以告诉我这个错误究竟意味着什么。它提到'从字符串“BT”转换为'Boolean'类型无效。其中'BT'是正确的用户名。

Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        ' If txtLogin.Text = "1" And txtPassBox.Text = "1" Then
        ' Form2.Show()
        ' Me.Hide()


        ' Else : MsgBox("Sorry, That combination is not recognised.", MsgBoxStyle.Critical, "Invalid Data Supplied")

        ' End If

        Dim user As String
        Dim password As String

        user = txtLogin.Text
        password = txtPassBox.Text

        If Me.UserTableAdapter.ScalarQueryLogin(user, password) Then
            MessageBox.Show("You have logged in")
        Else
            MessageBox.Show("You have supplied the wrong combo")
        End If

    End Sub

sql查询:

SELECT        [User], [Password]
FROM            [User]
WHERE        ([User] = ?) AND ([Password] = ?)

2 个答案:

答案 0 :(得分:1)

看起来你的ScalarQueryLogin方法返回一个字符串,你正在使用它,好像它是一个布尔值。

编辑: 如果你告诉我们你的ScalarQueryLogin方法返回什么类型,那么解决这个问题会更容易。无论如何,尝试这样的事情:

Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        ' If txtLogin.Text = "1" And txtPassBox.Text = "1" Then
        ' Form2.Show()
        ' Me.Hide()


        ' Else : MsgBox("Sorry, That combination is not recognised.", MsgBoxStyle.Critical, "Invalid Data Supplied")

        ' End If

        Dim user As String
        Dim password As String

        user = txtLogin.Text
        password = txtPassBox.Text

        If Me.UserTableAdapter.ScalarQueryLogin(user, password) IsNot Nothing Then
            MessageBox.Show("You have logged in")
        Else
            MessageBox.Show("You have supplied the wrong combo")
        End If

    End Sub

答案 1 :(得分:1)

我很难完全回答这个问题,因为缺少大量数据,但是从错误和上下文中我会说ScalarQueryLogin正在返回一个字符串(特别是它返回{{1}没有看到正在进行的实际查询,很难确定。我建议可能是这样的:

user

这样您就可以逐步了解查询返回的内容,并可以为您提供有关问题的更多信息。