Results 1 to 26 of 26

Thread: [RESOLVED] [2008] user login

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Resolved [RESOLVED] [2008] user login

    i am trying to log a user in using a login form. i have my connection to my database and there and it connects just fine but it will only read the first row. here is my code.
    Code:
            Dim ConnString As String = "Driver={MySQL ODBC 3.51 Driver};Server=dansoft.selfip.com;Option=131072;Stmt=;Database=elemental_battle; User=****;Password=*****;"
            Dim objConn As New OdbcConnection(ConnString)
    
            objConn.Open()
            Dim dt As New DataTable
            Dim sqlComm As New OdbcCommand("SELECT username,password from userinfo", objConn)
            Dim dr As OdbcDataReader = sqlComm.ExecuteReader
            dr.Read()
       
            Dim user As String = CStr(dr("username"))
            Dim pass As String = CStr(dr("password"))
    
            If user = UsernameTextBox.Text And pass = PasswordTextBox.Text Then
                frmMain.Show()
                Me.Close()
            Else
                MessageBox.Show("Invalid Username or password, Please try again")
            End If
            objConn.Close()
    there are two entries in the database. the first username and password are bagstoper and 1a2b3c4 the second is bagstoper2 and 1a2b3c4d. when i test it with the first username and password it works but when i try the second it doesnt work. so any help would be great. thank you.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] user login

    You're only calling Read once so, logically, you're only reading one record. That said, there's no point bringing back every user name and password when all you really care about is if there is one that matches. The database can tell you that. Follow the Validate Credentials link in my signature for an example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] user login

    I'd also suggest that you download the free ADO.NET provider from MySQL and use that rather than using Odbc.

    http://dev.mysql.com/downloads/connector/net/5.2.html
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i tried the link in your sig but it failed every time when i knew the userID and password were correct.
    Code:
    Dim connection As New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=dansoft.selfip.com;Option=131072;Stmt=;Database=elemental_battle; User=****;Password=****;")
            Dim command As New OdbcCommand("SELECT COUNT(*) FROM userinfo WHERE username = @UserID AND password = @Password", connection)
    
            With command.Parameters
                .AddWithValue("@UserID", UsernameTextBox.Text)
                .AddWithValue("@Password", PasswordTextBox.Text)
            End With
            connection.Open()
            If CInt(command.ExecuteScalar()) = 0 Then
                MsgBox("Invalid Username or Password.")
            Else
                frmMain.Show()
                Me.Hide()
            End If
            connection.Close()
    maybe there was something wrong with my modified version of your code.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] user login

    MySQL doesn't prefix parameters with "@". I believe that MySQL uses "?". You may be able to just replace all the "@" with "?", or you may have to remove the entire parameter names from the SQL code and use just "?" on its own.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    got an error.
    Code:
    ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.51a]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UserID AND password = '1a2b3x4'Password' at line 1
    that is when i changed the '@'s to'?'s

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i checked the documentation and it is still an @ symbol not a ?

  8. #8
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    Had you try "?" only instead of "?UserID"

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i am having trouble with the ADO.NET provider from MySQL. i have to create views and i have never done that. this is my view command code.
    Code:
    @UserID=usernametextbox.text
    @password=passwordtextbox.text
    SELECT COUNT(*) FROM userinfo WHERE username = @UserID AND password = @Password
    but it still says that i need to declare @userID

  10. #10
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    On #3 reply above by Jm, there is a suggestion there for you to download.Have you try that yet?

    Also, try to make your query in the TableAdapter and try to observe there what really is an issue.Honestly, I can't give an accurate solution as much as I want since I am using MS SQL Server.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    Quote Originally Posted by aldrean
    Had you try "?" only instead of "?UserID"
    yes i tried ?userId and ?password

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    Quote Originally Posted by aldrean
    On #3 reply above by Jm, there is a suggestion there for you to download.Have you try that yet?
    yes i have downloaded it that is why i need the info on the create view stuff because that is how it gets the data at least that is what the documentation says.
    Quote Originally Posted by aldrean
    Also, try to make your query in the TableAdapter and try to observe there what really is an issue.Honestly, I can't give an accurate solution as much as I want since I am using MS SQL Server.
    how do i make a table adapter i used to know how to do it but i cant remember now.

  13. #13
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    Firstly, you have to create a DataSet.On you project add an item and select DataSet,name the DataSet whatever you want.Now it will give you a blank dataset now(grey color),you have to right click on this and select Add TableAdapter.It will prompt you to make a connection to the database,and if your successfull, you can select which specific table/s you want to to load on your adapter.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    ok i have a table adapter but how do i use it to check the username and password?

  15. #15
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    Well, lets see, I am not keen in explaining details but I will try.

    I don't have idea which table you had selected in making the TableAdapter but I would assume that its the one where you can find the UserName and Password fields.Now,on your dataset you will see your table adapter right, so select the adapter and right click to it and select configure.It will give you the TableAdapter Configuration Wizard, now select Query Builder on the lower right side of the wizard and it will show you the table you had selected as well the sql statement that you have.

    The best way to check you query earlier is to put on the Select statement section what you have earlier on you post, and from there, try to click the Execute Query button.See what goes wrong from there.
    For sure, that would prompt you for error, now you have to experiment which parameter sign you have to use, if its ? then go and check, otherwise try the @.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i tried it and it didn't work. i think it is because i need to tell it what @userid and @password are but i cant since it isnt being done at run time. i have to have this code for it to return any data. even though it is wrong.
    Code:
          With command.Parameters
                .AddWithValue("@UserID", UsernameTextBox.Text)
                .AddWithValue("@Password", PasswordTextBox.Text)
            End With

  17. #17
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    Ok, forgot to tell you, on the TableAdapter Configuration wizard you can set there the parameter by for instance, on the UserName field there is a Filter field there, try to have it with =@UserName, same with the Password field try to have it with =@Password.

    Executing the query will prompt you to supply both parameters and if your values are correct meaning they are in the table, the query will return you with values.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    yes but how do i do that at runtime?

  19. #19
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    274

    Re: [2008] user login

    May I ask what happened to your query, I mean in the DataAdapter does it return you with values upon supplying with the right username and password?I yes then you can start modifying the code given by Jm.Remember, I just use the adapter to check if your problems deals with the parameter or something.Although you can still use the table adapter, but making an execute scalar is way simple compared to having a Fill method for your adapter.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    ok it might work but once i have built the table adapter with the command in it how do i call the command? this might work so far it looks like a sub that would have overloads. ScalarQuery (@UserID, @Password)

  21. #21
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    Re: [2008] user login

    hi
    use a loop to check thr' all the records in the table

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i had thought about that but how would i index the loop ( i know how to make a loop but not for this)

  23. #23
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    Re: [2008] user login

    wht excatly u need? u want that when a user logins his username & password both shld match from the database & if both correct shld be able to login.
    then use a loop to check thr' the existing records. if both matches then u can login

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    my point is i cant do a datareader.read(i) where I is the record. i have tried that. that was my first idea.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    i tried to change my data base to sql express but i cant seem to get it to connect becuase it requires me to set it up for remote connections (which i did) but for some reason it isnt working. it is giving me this error.

    An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] user login

    ok nevermind this thread is resolved. this is the working code.
    Code:
             Dim ConnString As String = "Driver={MySQL ODBC 3.51 Driver};Server=dansoft.selfip.com;Option=131072;Stmt=;Database=elemental_battle; User=****;Password=****;"
            Dim objConn As New OdbcConnection(ConnString)
    
            objConn.Open()
    
            Dim sqlComm As New OdbcCommand("SELECT username,password from userinfo", objConn)
            Dim dr As OdbcDataReader = sqlComm.ExecuteReader
    
            Do While dr.Read
                Dim user As String = CStr(dr("username"))
                Dim pass As String = CStr(dr("password"))
                If user = UsernameTextBox.Text And pass = PasswordTextBox.Text Then
                    frmMain.Show()
                    Me.Close()
                    Exit Sub
                End If
            Loop
            MsgBox("Invalid Username or password, Please try again")
            objConn.Close()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width