Click here to Skip to main content
15,891,852 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I getting the error "Incorrect Username or Password" for all input.

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

       
        Dim con As New SqlConnection("Data Source=192.168.10.3;Initial Catalog=IT_INV;user id=sa;password=1")
        con.Open()
        Dim dt As New DataTable("user")
        Dim rs As New SqlCommand("SELECT * FROM [user] WHERE username='" & TextBox1.Text & "' AND passw='" & TextBox2.Text & "'", con)

            Dim usernameParam As New SqlParameter("username", Me.TextBox1.Text)
            Dim passwordParam As New SqlParameter("passw", Me.TextBox2.Text)

            rs.Parameters.Add(usernameParam)
            rs.Parameters.Add(passwordParam)

            Dim sqlRead As SqlDataReader = rs.ExecuteReader
            If sqlRead.HasRows Then
                If sqlRead.Read = True Then


                    If sqlRead("usertype") = "admin" Then
                        MsgBox("admin")
                    ElseIf sqlRead("usertype") = "user" Then
                        MsgBox("user")

                    Else
                        MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                        TextBox1.Text = ""
                        TextBox2.Text = ""

                    End If

                End If
            End If

            con.Close()

  Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Posted
Updated 18-May-15 17:52pm
v2

1 solution

The solution to it is obvious. You are running an SQL command which is supposed to select the user where the conditions are met. Otherwise the results won't be presented to you.

After executing all of the statements, you are looking into the data reader to check for another column value of that user. Which is, "userType".

You should know that if there is no record found then the value is not matched with either admin or user. That is why, you will always get this message. I know you cannot minimize this error, and so there is no solution. Unless there is a result in the database for this query, and the user would not be matched. To ensure that this error doesn't show up again, make sure,


  1. You have at least one record in your database table
  2. User and password fields are matching with the input you provide (for testing)
  3. userType field is also either "admin" or "user"


Then I believe that this code would work and would return the result that you want to see.

Tip: Never stored passwords in plain-text. Hash them and store the hash in the database.

Edit

The problem is that you have replaced the value... In the query if you look,

VB
Dim rs As New SqlCommand("SELECT * FROM [user] WHERE username='" & TextBox1.Text & "' AND passw='" & TextBox2.Text & "'", con)
 
Dim usernameParam As New SqlParameter("username", Me.TextBox1.Text)
Dim passwordParam As New SqlParameter("passw", Me.TextBox2.Text)


You meant to write the query this way,

VB
Dim rs As New SqlCommand("SELECT * FROM [user] WHERE username=@0 AND passw=@1", con)
 
Dim usernameParam As New SqlParameter("0", Me.TextBox1.Text)
Dim passwordParam As New SqlParameter("1", Me.TextBox2.Text)


This is the parameterized query. Which is now going to be passed as the value for the condition. Run the query now and see the result.
 
Share this answer
 
v2
Comments
veena15 19-May-15 1:27am    
I have tried all you have mentioned.

My database look like this :

username | passw | usertype
----------------------------
user | user1 | user
admin | admin1 | admin
Afzaal Ahmad Zeeshan 19-May-15 1:48am    
Have a look at the answer now in the Edit section.
veena15 19-May-15 1:51am    
Still the same result..
Afzaal Ahmad Zeeshan 19-May-15 1:52am    
Then you need to debug your application. :-)
veena15 19-May-15 1:59am    
No changes. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900