Results 1 to 7 of 7

Thread: [2008] Retrieve username on login

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    [2008] Retrieve username on login

    I just created a login form using the following code. This code works.

    But now i want to retrieve the name (gebruikersnaam) and ID (medewerker_ID) of the user that just logged in.

    I added the test oledbcommand but how can I retrieve the name.

    I have a public variable declared named strUsername.

    Code:
    myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDatabasePath
        Dim cmd As New OleDbCommand("SELECT COUNT(*) FROM tblMEDEWERKER WHERE GebruikersNaam = @UserName AND Wachtwoord = @Password AND GebruikerApplicatie=true", myConnection)
    
        Dim test As New OleDbCommand("SELECT Achternaam FROM tblMEDEWERKER WHERE GebruikersNaam = @UserName", myConnection)
        
       
        cmd.Parameters.AddWithValue("@UserName", txtUsername.Text)
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text)

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

    Re: [2008] Retrieve username on login

    There's no point executing a query that counts the records. You would do that if you were only interested in whether there was a matching record or not. In your case you actually want the data that the matching record contains. You should be executing one query only. That query should select the columns that you're interested in WHERE the user name and password are the values supplied by the user.

    To execute the query you'd call ExecuteReader on your command to create a DataReader. You call Read on the DataReader and that will tell you whether there's a matching record or not. If it's True then you get the data for the record from the DataReader.

    Follow the Data Access link in my signature for ADO.NET example code that includes using a DataReader.
    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

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: [2008] Retrieve username on login

    Quote Originally Posted by jmcilhinney
    There's no point executing a query that counts the records. You would do that if you were only interested in whether there was a matching record or not. In your case you actually want the data that the matching record contains. You should be executing one query only. That query should select the columns that you're interested in WHERE the user name and password are the values supplied by the user.

    To execute the query you'd call ExecuteReader on your command to create a DataReader. You call Read on the DataReader and that will tell you whether there's a matching record or not. If it's True then you get the data for the record from the DataReader.

    Follow the Data Access link in my signature for ADO.NET example code that includes using a DataReader.
    Thank J.
    I tried the code in your link and I adjusted it like this:

    Code:
    myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDatabasePath
        
        Dim test As New OleDbCommand("SELECT Achternaam, Medewerker_ID FROM tblMEDEWERKER WHERE GebruikersNaam = @UserName", myConnection)
        
       
        cmd.Parameters.AddWithValue("@UserName", txtUsername.Text)
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    
        Dim reader As OleDbDataReader = test.ExecuteReader()
        While reader.Read()
          strIngelogdeUserNaam = reader("Achternaam")
          intIngelogdeUser_ID = reader("Medewerker_ID")
        End While
        reader.Close()
    When I run the code I get an erromessage on the line "Dim reader As OleDbDataReader = test.ExecuteReader()" saying: "Value for one or more parameters are missing".

    What am I doing wrong?
    Last edited by bodylojohn; Apr 4th, 2008 at 04:27 PM.

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

    Re: [2008] Retrieve username on login

    Code:
        Dim test As New OleDbCommand("SELECT Achternaam, Medewerker_ID FROM tblMEDEWERKER WHERE GebruikersNaam = @UserName", myConnection)
        
       
        cmd.Parameters.AddWithValue("@UserName", txtUsername.Text)
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
    
        Dim reader As OleDbDataReader = test.ExecuteReader()
    You need to add the parameters to the command you're executing.
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: [2008] Retrieve username on login

    Quote Originally Posted by jmcilhinney
    You need to add the parameters to the command you're executing.
    Thanks J.

    I studied your posts and I dont get that part.

    Could you please point me in the right direction?

    Thanks in advance

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

    Re: [2008] Retrieve username on login

    Oh come on. I even used colours. You create an OleDbCommand object and assign it to the 'test' variable. You then add parameters to a completely different OleDbCommand assigned to the 'cmd' variable. Finally you execute the OleDbCommand assigned to the 'test' variable. You're executing one command when you added parameters to a completely different command. If you're going to execute 'test' then you need to add parameters to 'test', not 'cmd'.
    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

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: [2008] Retrieve username on login

    Quote Originally Posted by jmcilhinney
    Oh come on. I even used colours. You create an OleDbCommand object and assign it to the 'test' variable. You then add parameters to a completely different OleDbCommand assigned to the 'cmd' variable. Finally you execute the OleDbCommand assigned to the 'test' variable. You're executing one command when you added parameters to a completely different command. If you're going to execute 'test' then you need to add parameters to 'test', not 'cmd'.
    Oh my god!!!!!

    I see my mistake...
    I have been trying and trying and thanks to your last post J I got it fixed...

    Thank you very very much.
    Again I've learned a lot from you....

    Untill the next time ;-)

    Again..thanks for making my weekend...

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