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
This should go in your Try block
And in the Finally block