user1532468
user1532468

Reputation: 1751

vb 2010 InvalidCastException error

I am trying to create a simple access db login form and have received the error above when running. This only happens if the user combo is correct. If the login is incorrect, then the invalid message is shown. But if I supply the correct credentials then this error is thrown. Can someone enlighten me as to exactly what this error means. It mentions 'conversion from string "BT" to type 'Boolean' is not valid.' where 'BT' is the correct username.

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 query:

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

Upvotes: 0

Views: 434

Answers (2)

Douglas Barbin
Douglas Barbin

Reputation: 3615

It looks like your ScalarQueryLogin method returns a string, and you are using it as if it were a Boolean.

EDIT: It would be easier to troubleshoot this if you told us what type your ScalarQueryLogin method returns. Anyways, try something like this:

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

Upvotes: 1

mageos
mageos

Reputation: 1291

It is hard for me to completely answer the question because there is a lot of data missing, but from the error and context I would say that ScalarQueryLogin is returning a string (specifically it is returning user. Without seeing that actual query that is being made it is hard to know for sure. I would suggest maybe something like this:

Dim resultValue as Object

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

If resultValue Then
...

This will allow you to step through and get a better idea of what your query is returning and may provide you with more insight about the problem.

Upvotes: 1

Related Questions