Vb.net vb 2010 InvalidCastException错误

Vb.net vb 2010 InvalidCastException错误,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,我试图创建一个简单的access db登录表单,在运行时收到了上面的错误。只有当用户组合正确时才会发生这种情况。如果登录不正确,则显示无效消息。但如果我提供了正确的凭据,则会抛出此错误。有人能告诉我这个错误的确切含义吗。它提到“从字符串“BT”到类型“Boolean”的转换无效。”其中“BT”是正确的用户名 Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn

我试图创建一个简单的access db登录表单,在运行时收到了上面的错误。只有当用户组合正确时才会发生这种情况。如果登录不正确,则显示无效消息。但如果我提供了正确的凭据,则会抛出此错误。有人能告诉我这个错误的确切含义吗。它提到“从字符串“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] = ?)

看起来您的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
Dim resultValue as Object

resultValue = Me.UserTableAdapter.ScalarQueryLogin(user, password)

If resultValue Then
...

我很难完全回答这个问题,因为有很多数据丢失,但从错误和上下文来看,我会说,
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
Dim resultValue as Object

resultValue = Me.UserTableAdapter.ScalarQueryLogin(user, password)

If resultValue Then
...

这将使您能够更好地了解您的查询返回的内容,并且可以为您提供更多关于问题的洞察力。

道格拉斯,我如何使用它作为布尔值?我设置了一个SQL查询,它没有错误地建立了OK。我是来自PHP背景的VB的新手。在您的if语句中,您可能需要考虑更安全的L。ogon通常使用散列之类的方法来代替存储用户密码,但为了解决当前的问题,您可以尝试以下查询:
SELECT COUNT(*)FROM[User]WHERE([User]=?)和([Password]=?)
然后,如果resultValue>0,您可以进行
比较,因为查询将返回一个整数,而不是传入的用户名。没有查询mageos。谢谢我有点困惑,因为我在youtube上学习了一个教程,他们计算了(*)在语句外,查询运行正常。我只是想理解逻辑。Thanks@user1532468此链接将很有帮助,只需将查询从[User]更改为
选择count(*),其中([User]=?)和([Password]=?)
则应转换为布尔值