RustyGutu
RustyGutu

Reputation: 1

Why can't my function return a true value?

I get an error saying:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Additional information: Conversion from string "Admin" to type 'Boolean' is not valid.

When I run the program putting in the correct values for username and password I don't get the error, when I put in the wrong values the If statement proceeds correctly. I'm remade the program countless time and still get this error. Here is my code:

Public Class Login
Dim Password As String
Dim Username As String

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    //TODO: This line of code loads data into the "DatabaseADataSet.Logon" table. You can move, or remove it, as needed.
    Me.LogonTableAdapter.Fill(Me.DatabaseADataSet.Logon)

End Sub

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

    Username = txtUsername.Text
    Password = txtPassword.Text
    CheckUser(Username, Password)
End Sub


Function CheckUser(Username As String, Password As String) As String

    If Me.LogonTableAdapter.ScalarQuery(Username, Password) = True Then
        MessageBox.Show("Login Succesful")
    Else
        MessageBox.Show("Login unsuccesful, would you like to register", "Unsuccesful login.", MessageBoxButtons.YesNo)
        If DialogResult.Yes Then
            frmRegister.Show()
        End If
    End If
End Function
End Class

Upvotes: 0

Views: 186

Answers (4)

RustyGutu
RustyGutu

Reputation: 1

Well I fixed the problem for anyone looking in the future. I changed the if statement to see if the value is nothing or not.

If Me.LogonTableAdapter.ScalarQuery(Username, Password) IsNot Nothing Then

Upvotes: 0

andrejke
andrejke

Reputation: 56

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx

It returns the first column of the first row in the result set or Null Reference. The latter can be converted to False but the content of the given cell can't be converted to True.

Upvotes: 0

Keith
Keith

Reputation: 1321

What does ScalarQuery return? A string for true/false?

If so, then compare with "true" or "false":

If Me.LogonTableAdapter.ScalarQuery(Username, Password) = "True" Then

As others have stated, you should probably turn on OPTION STRICT

Upvotes: 0

InbetweenWeekends
InbetweenWeekends

Reputation: 1414

It would be more helpful, as Tim mentioned (+1 for Option Strict), to know what ScalarQuery does.

Going under the assumption that your ScalarQuery performs an ExecuteScalar on a SQL query, it will return the first column of your query. In this case, it looks like you passed in 'Admin' and it's corresponding password. Since your supplied query is selecting the Username column as the first ordinal, that's what your ScalarQuery is returning. Since "Admin" (string) cannot cast to True (boolean) - you get the error you posted.

Given what you've posted, you'd want to either structure your SQL query to return a boolean value which can cast, or change your IF conditional to apply the correct logic.

Upvotes: 1

Related Questions