locked
MySQL Login Problems.. RRS feed

  • Question

  • Hey there again! :)

    This time it's an question about MySQL... :)

    Well, i have been searching a litle here but havnt found a good solution that works (for me).

    Okey, well, ive got Database wich i can Connect to and retrieve information and save information to etc..

    BUT, my problem is that, i do have a login form here aswell wich i want to use as a login to the database,

    For example, i have 2 textboxes, (txtUsername, txtPassword) on the form, and when i click "cmdOK" button it will see if the username and password matches the ones in my database, for example

    If username.text = ("SELECT * FROM users WHERE username='" & txtUsername.text & "'", HostConn) Then
     LoginSuccesfully etc..
    Else
     MsgBox("error... you failed totaly bla bla bla")
    EndIf

    --

    So... here goes my question, How do i do that? :)
    See if the txtUsername and txtPassword matches the ones in my database (database tables: username, password)

    I am more then happy to post the source code here (its only a few lines.. :) ) if that would help :)

    I appologize for the one who spend their time to help me out on this day... (Happy easter-egg-time-or-what-its-called) :P

    Anyway, thanks for your time and Happy Easter.. (Dont realy know what it actualy is, is it "Happy Easter time(?)" or.. "Happy Easter"? I blame the beer...

    Regards, Micke aka RoteX

    Saturday, April 3, 2010 4:48 PM

Answers

  • Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=MySQL;" '
            Dim conn As New OdbcConnection(connectionString)
            conn.Open()
            Dim cmd As New OdbcCommand("SELECT TOP(1) FROM Users WHERE UserName =" + txtUserName.Text + " Password = " + txtPassword.Text)
            Dim reader As OdbcDataReader = cmd.ExecuteReader()
            If reader.HasRows Then
                'Login Success
            Else
                'Login Failed
            End If
            reader.Close()
            cmd.Dispose()
            conn.Close()

    The above code is just for demonstration. Never hardcode queries, either use stored procedures or parameterized queries.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Proposed as answer by The Webo Saturday, April 3, 2010 10:08 PM
    • Marked as answer by RoteX Sunday, April 4, 2010 9:42 AM
    Saturday, April 3, 2010 5:24 PM
  • If myData.HasRows = 0 Then
    
       MsgBox("Invalid login")
    
    Else
    
       Dim avatarName As String = myData("avatarname").ToString()   'assuming avatarname is the exact column name
    
       lblSomething.Text = avatarName
    
    End If

    How this is working?

    The query we are executing is, get the rows with username = username and password = password.

    If the login succeeds then we will get atleast one row with username, password, avatarname.

    Now we have that row data in myData(which is a DataReader object). In the datareader object just pass the column name whose value you want. It will be returning an object, convert it to string and use it.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by RoteX Sunday, April 4, 2010 9:41 AM
    Sunday, April 4, 2010 3:57 AM

All replies

  • Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=MySQL;" '
            Dim conn As New OdbcConnection(connectionString)
            conn.Open()
            Dim cmd As New OdbcCommand("SELECT TOP(1) FROM Users WHERE UserName =" + txtUserName.Text + " Password = " + txtPassword.Text)
            Dim reader As OdbcDataReader = cmd.ExecuteReader()
            If reader.HasRows Then
                'Login Success
            Else
                'Login Failed
            End If
            reader.Close()
            cmd.Dispose()
            conn.Close()

    The above code is just for demonstration. Never hardcode queries, either use stored procedures or parameterized queries.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Proposed as answer by The Webo Saturday, April 3, 2010 10:08 PM
    • Marked as answer by RoteX Sunday, April 4, 2010 9:42 AM
    Saturday, April 3, 2010 5:24 PM
  • Thank you for your help A.m.a.L - aditi.com - Think Product.

    It kinda works now, i can login now with my username & password from my database.

       -- Got another problem now kinda, now, for example if do have a database structure like this:
         
          databasename: users

          username
          password
          avatarname

    If i succesfully login (wich i can now etc..) i want my "Label1" on my "MainFrame" to show its Username -> Avatarname

    For example, my Username is = "user1" and my Avatarname is "test1".
    How do i make that my Label1 shows my Usernames Avatarname if the login is sucessfully?

    Heres my code right now that works

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
    
            Dim conn As MySqlConnection
    
            conn = New MySqlConnection()
            conn.ConnectionString = "server=localhost; user id=root; password=; database=entropia_tracker"
    
            Try
                conn.Open()
            Catch myerror As MySqlException
                MsgBox("Error connection to database")
            End Try
    
            Dim myAdapter As New MySqlDataAdapter
    
            Dim LoginCheck = "SELECT * FROM users WHERE username = '" + txtUser.Text + "' AND password= '" + txtPass.Text + "'"
    
            ' User settings
    
    
    
            Dim myCommand As New MySqlCommand
            myCommand.Connection = conn
            myCommand.CommandText = LoginCheck
    
            myAdapter.SelectCommand = myCommand
            Dim myData As MySqlDataReader
            myData = myCommand.ExecuteReader
    
            If myData.HasRows = 0 Then
                MsgBox("Invalid login!")
                'failed to login
            Else
                MsgBox("Login succesfully!")
                'show form etc
    
                frmMain.Show()
                frmMain.lblUserName.Text = txtUser.Text
    
                Me.Hide()
            End If
    
    
        End Sub
    If someone can help me out here would be awsome! :)

    Regards,
    Micke aka RoteX
    Saturday, April 3, 2010 11:09 PM
  • If myData.HasRows = 0 Then
    
       MsgBox("Invalid login")
    
    Else
    
       Dim avatarName As String = myData("avatarname").ToString()   'assuming avatarname is the exact column name
    
       lblSomething.Text = avatarName
    
    End If

    How this is working?

    The query we are executing is, get the rows with username = username and password = password.

    If the login succeeds then we will get atleast one row with username, password, avatarname.

    Now we have that row data in myData(which is a DataReader object). In the datareader object just pass the column name whose value you want. It will be returning an object, convert it to string and use it.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by RoteX Sunday, April 4, 2010 9:41 AM
    Sunday, April 4, 2010 3:57 AM
  • If you don't have a login problem then why are you still going on with this thread,

    Mark the answers which helps you as answer and create a new question


    Success
    Cor
    Sunday, April 4, 2010 5:22 AM
  • If myData.HasRows = 0 Then
    
    
    
       MsgBox("Invalid login")
    
    
    
    Else
    
    
    
       Dim avatarName As String = myData("avatarname").ToString()   'assuming avatarname is the exact column name
    
    
    
       lblSomething.Text = avatarName
    
    
    
    End If
    
    

    How this is working?

    The query we are executing is, get the rows with username = username and password = password.

    If the login succeeds then we will get atleast one row with username, password, avatarname.

    Now we have that row data in myData(which is a DataReader object). In the datareader object just pass the column name whose value you want. It will be returning an object, convert it to string and use it.

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code


    Thank you very much, again!

    It works perfect now, just needed to add a myData.Read() or else i got an error. :)

     

    Cor Lighert,
    Well i did have a problem with my login but A.m.a.L helped me with it so, then i needed help with another function here related to my mysql login aswell.
    So why start 2 topics on the pretty much same topic? :)

    Sunday, April 4, 2010 9:41 AM
  • RoteX, I am so happy to see your problem got resolved, :-)

    As you can see similar to you many users are facing similar issues. So if we don't cross post this will help the other user to find the solution easily from the thread. CorLighert, suggested to start a new thread to avoid this cross posting. So our msdn forum will be much cleaner and easier to find solutions.

    In this case you can easily split it into two threads.

    1. Using MySql how to validate username and password

    2. How to read a datarow from MySql

    Hope this makes sense.

    Happy coding!!!


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Sunday, April 4, 2010 9:50 AM