[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)
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.
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?
Re: [2008] Retrieve username on login
Quote:
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.
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
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'.
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...