I'm trying to retrieve record data from an MS-Access database and store it into a variable.
I'm using this SQL command to query the database:
Dim cmdRead As OleDbCommand = New OleDbCommand("SELECT UserID FROM [User] where Username=?", conn)
How do I do it in VB.NET to read data from the UserID column (in the db) and store it into a variable in VB.NET?
What I basically want to do is to retrieve the UserID so that I can then retrieve the Name and Surname of the user from the same table.
Note: I'm using ASP.NET with Web Develop 2003 and an MS-Access 2003 db.
This is the full code:
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT Username, Password FROM [User] where Username=? and Password=?", conn)
cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
Try
conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
While read.Read()
If txtUsername.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then
Dim tID As Long = read.Item("UserID").ToString
Dim tName As String = read.Item("CustomerName").ToString
Dim tSurname As String = read.Item("CustomerSurname").ToString
lblLogged.Text = lblLogged.Text + tName + tSurname
lblLogged.Visible = True
End If
End While
Else
lblLogged.Text = "Login unsuccessful"
End If
read.Close()
Catch ex As Exception
Response.Write(ex.Message())
Finally
conn.Close()
End Try
End Sub
End Class
Something is out of order in your example. Please try this.
First change the OleDbCommand to retrieve also the userID, no need for a second command when your read from the same table and I suppose that UserName and Password identify an unique user
Dim cmd As OleDbCommand = New OleDbCommand("SELECT UserID, Username, Password FROM [User] where Username=? and Password=?", conn)
then add your parameters and query the database
Try
conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
read.Read()
' Now you can read the Item and pass the values to your textboxes
txtUsername.Text = read.Item("username").ToString
txtPassword.Text = read.Item("password").ToString
txtUserID.Text = read.Item("userid").ToString
Else
lblLogged.Text = "Login unsuccessful"
End If
read.Close()
Catch ex As Exception
Response.Write(ex.Message())
Finally
conn.Close()
End Try
This should go in your Try block
conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
While read.Read()
If txtUsername.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then
Dim tID as long = read.Item("UserID").ToString
Else
lblLogged.Text = "Login unsuccessful"
End If
End While
End If
And in the Finally block
read.Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.