Imports System.Data
Imports System.Data.OleDb
Imports System.Data.DataTable
Public Class Form1
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Katlego\Documents\LoginDb.accdb")
Dim cmd As New OleDbCommand("select * from logintable where username1=@username1 and password1=@password1, conn")
cmd.Parameters.Add("@username1", oleDbType:=OleDbType.VarChar).Value = txtusername.Text
cmd.Parameters.Add("@password1", oleDbType:=OleDbType.VarChar).Value = txtpassword.Text
Dim adapter1 As New OleDbDataAdapter(cmd)
Using logintable As New DataTable
Dim unused = adapter1.Fill(logintable)
If logintable.Rows.Count <= 0 Then
MsgBox("error username or password")
Else
MsgBox("login sucessfull")
End If
End Using
End Sub
CodePudding user response:
You need to open the connection first
conn.Open()
CodePudding user response:
There's a typo when you are initializing cmd
object. You include the conn object inside the connection string. So the cmd object is missing a connection object.
Dim cmd As New OleDbCommand("select * from logintable where username1=@username1 and password1=@password1, conn")
Should be:
Dim cmd As New OleDbCommand("select * from logintable where username1=@username1 and password1=@password1", conn)