Bento
Bento

Reputation: 5

Cannot connect to Access using VB.net

i want to create a login page for my application. and when everything has finished and i want to login it always shows

Error :

The Microsoft Jet database engine cannot open the file 'C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug'. It is already opened exclusively by another user, or you need permission to view its data.

Code :

Imports System.Data.OleDb

Public Class Login
    Dim path = System.Windows.Forms.Application.StartupPath
    Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub

Private Sub loginbtn_Click(sender As Object, e As EventArgs) Handles loginbtn.Click
    Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug;")
    Dim command As New OleDbCommand("SELECT [ID] FROM [User] WHERE [usernameField] = Username AND [passwordField] = Password", connection)

    Dim usernameparam As New OleDbParameter("Username", Me.usernamebox.Text)
    Dim passwordparam As New OleDbParameter("Password", Me.passwordbox.Text)

    command.Parameters.Add(usernameparam)
    command.Parameters.Add(passwordparam)
    command.Connection.Open()
    Dim reader As OleDbDataReader = command.ExecuteReader()
    If reader.HasRows Then
        MessageBox.Show("Login Succesful!")
        passwordbox.Text = ""
        Me.Hide()
        Main.Show()

    Else
        MessageBox.Show("Username and Password are incorrect!")
        passwordbox.Text = ""
    End If

    command.Connection.Close()
End Sub


Private Sub exitbtn_Click(sender As Object, e As EventArgs) Handles exitbtn.Click
    Me.Close()
    End Sub
End Class

Upvotes: 0

Views: 1456

Answers (1)

Jade
Jade

Reputation: 2992

Change this line

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug;")

to

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug\YourMSAccessDB.mdb;")

or to load the db in your output directory

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\YourMSAccessDB.mdb;")
'".\" is equivalent to your output directory or where your application (exe file) is located.

Upvotes: 1

Related Questions