3 Replies - 861 Views - Last Post: 16 May 2016 - 02:29 AM

#1 alobi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

I am having problem connecting to access database

Posted 13 May 2016 - 03:30 PM

I am trying to connect to access database but each time I will get this error message"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll" at the underlined code


Public Class frmlogin
    Dim provider As String
    Dim datafile As String
    Dim connstring As String
    Dim myconnection As OleDbConnection = New OleDbConnection
    Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
        provider = "provider = Microsoft.ACE.OLEDB.12.0; Data Source="
        datafile = "C:\Vb.Net Security\SecurityGuard.accdb"
        connstring = provider & datafile
        myconnection.ConnectionString = connstring
        'the query 
        [u][color="#000080"]myconnection.Open()[/[/color]u]
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [userstbl] WHERE [username] = '" & txtusername.Text & "' AND [password] = '" & txtpassword.Text & "'", myconnection)
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        ' The following variable hold true if user is found atherwise it holds false
        Dim userFound As Boolean = False
        Dim Username As String = ""
        Dim Password As String = ""
        ' If found
        While dr.Read
            userFound = True
            Username = dr("Userna").ToString
            Password = dr("Password").ToString
        End While
        'checking the result

        If userFound = True Then

            frmMain.Show()

        Else

            MsgBox("Sorry, username or password not found", MsgBoxStyle.OkOnly, "Invalid Login")

        End If
        myconnection.Close()

    End Sub

    Private Sub frmlogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class


This post has been edited by macosxnerd101: 13 May 2016 - 03:35 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I am having problem connecting to access database

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5061
  • View blog
  • Posts: 13,657
  • Joined: 18-April 07

Re: I am having problem connecting to access database

Posted 13 May 2016 - 05:28 PM

What version of access is the database? Does it require a username and password to open? I suggest you try merging things together a little to hopefully simplify and avoid any problems you may have introduced...

Dim myconnection as new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Vb.Net Security\SecurityGuard.accdb;Persist Security Info=False")
myconnection.Open()



See if that works. If it does, it might just be how you are putting it together that it was having a problem with. :)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,570
  • Joined: 12-December 12

Re: I am having problem connecting to access database

Posted 14 May 2016 - 01:25 AM

An important reference,

Access connection strings
Was This Post Helpful? 0
  • +
  • -

#4 alobi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Re: I am having problem connecting to access database

Posted 16 May 2016 - 02:29 AM

View PostMartyr2, on 13 May 2016 - 05:28 PM, said:

What version of access is the database? Does it require a username and password to open? I suggest you try merging things together a little to hopefully simplify and avoid any problems you may have introduced...

Dim myconnection as new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Vb.Net Security\SecurityGuard.accdb;Persist Security Info=False")
myconnection.Open()



See if that works. If it does, it might just be how you are putting it together that it was having a problem with. :)/>/>


Thanks Martyr2
The version of Access is 2007, and yes it does use username and password to open
Here is the error message I am getting
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: The ConnectionString property has not been initialized".
Thank you for your time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1