Here is some code I am using to connect to an oleDbConnection database. I am writing a routine to force the user to enter a user id and password. Both must match what is in a .mdb table. The database is in a folder with my project. I am okay until I get to Cnxn(open) I get an unhandled exception error. Do I need to put in a try catch finally routine here or is the problem with installation of the application (ie missing .dll file?)
Imports System.Data.OleDb
Public Class frmAuthenticateUser
Inherits System.Windows.Forms.Form
Dim LoginAttempts As Integer
Private Sub frmAuthenticateUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoginAttempts = 0
lblAdvice.Text = "Enter your User Id and Password, then click the Log In button..."
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim SQLStmt, ValidationError As String
Dim MatchCounter As Integer
'Attach to the database
Dim Cnxn _
As New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=CommonAlgorithms.mdb")
Cnxn.Open() "This is where I get an unhandled exception error. system.data.dll"
If Cnxn.State <> ConnectionState.Open Then
MsgBox("Unable to connect to the database...", MsgBoxStyle.Critical)
End
End If
'Check the database and set MatchCounter
SQLStmt = String.Format( _
"SELECT count(Id) as MatchCounter FROM Accounts WHERE LoginId='{0}' and Password='{1}'", _
txtLoginId.Text, txtPassword.Text)
Dim HowManyMatches As New OleDbCommand(SQLStmt, Cnxn)
Dim Matches As OleDbDataReader = HowManyMatches.ExecuteReader
If Not Matches.Read Then
MatchCounter = 0
Else
MatchCounter = Matches.Item("MatchCounter")
End If
Cnxn.Close()
'Set ValidationError depending on MatchCounter
Select Case MatchCounter
Case 0
ValidationError = "Invalid Login Id and/or Password! These are both case sensitive. Correct the error and click the Log In button... "
Case 1
ValidationError = ""
Case Is > 1
ValidationError = "Please inform the systems administrator that the userid and password entered are duplicates."
End Select
If ValidationError <> "" Then
LoginAttempts += 1
If LoginAttempts > 2 Then
MsgBox("There have been three failed attempts to log on! Check with the systems administrator if you're unable to recollect your userid and password.", MsgBoxStyle.Exclamation)
End
End If
lblAdvice.ForeColor = ForeColor.DarkRed
lblAdvice.Text = ValidationError
Return
End If
'Setup new LoggedInUser object
Dim LoggedInUser As New LoggedInUser
SQLStmt = String.Format("SELECT Id, Bailiwicks, LastName, FirstName FROM Accounts WHERE LoginId='{0}' and Password='{1}'", txtLoginId.Text, txtPassword.Text)
Cnxn.Open()
Dim BailiwickCommand As New OleDbCommand(SQLStmt, Cnxn)
Dim AccountData As OleDbDataReader = BailiwickCommand.ExecuteReader
If Not AccountData.Read() Then
MsgBox("Unable to read your account data from the database at this time...", MsgBoxStyle.Critical)
End
End If
LoggedInUser.AccountNumber = AccountData.Item("Id")
LoggedInUser.Bailiwick = AccountData.Item("Bailiwicks")
LoggedInUser.WebPageName = AccountData.Item("FirstName") _
& " " & AccountData.Item("LastName")
Cnxn.Close()
'Setup new MainPanel and send them there
Dim MainPanel As frmMainPanel
MainPanel = New frmMainPanel
MainPanel.Show()
Me.Hide()
End Sub
Imports System.Data.OleDb
Public Class frmAuthenticateUser
Inherits System.Windows.Forms.Form
Dim LoginAttempts As Integer
Private Sub frmAuthenticateUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoginAttempts = 0
lblAdvice.Text = "Enter your User Id and Password, then click the Log In button..."
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim SQLStmt, ValidationError As String
Dim MatchCounter As Integer
'Attach to the database
Dim Cnxn _
As New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=CommonAlgorithms.mdb")
Cnxn.Open() "This is where I get an unhandled exception error. system.data.dll"
If Cnxn.State <> ConnectionState.Open Then
MsgBox("Unable to connect to the database...", MsgBoxStyle.Critical)
End
End If
'Check the database and set MatchCounter
SQLStmt = String.Format( _
"SELECT count(Id) as MatchCounter FROM Accounts WHERE LoginId='{0}' and Password='{1}'", _
txtLoginId.Text, txtPassword.Text)
Dim HowManyMatches As New OleDbCommand(SQLStmt, Cnxn)
Dim Matches As OleDbDataReader = HowManyMatches.ExecuteReader
If Not Matches.Read Then
MatchCounter = 0
Else
MatchCounter = Matches.Item("MatchCounter")
End If
Cnxn.Close()
'Set ValidationError depending on MatchCounter
Select Case MatchCounter
Case 0
ValidationError = "Invalid Login Id and/or Password! These are both case sensitive. Correct the error and click the Log In button... "
Case 1
ValidationError = ""
Case Is > 1
ValidationError = "Please inform the systems administrator that the userid and password entered are duplicates."
End Select
If ValidationError <> "" Then
LoginAttempts += 1
If LoginAttempts > 2 Then
MsgBox("There have been three failed attempts to log on! Check with the systems administrator if you're unable to recollect your userid and password.", MsgBoxStyle.Exclamation)
End
End If
lblAdvice.ForeColor = ForeColor.DarkRed
lblAdvice.Text = ValidationError
Return
End If
'Setup new LoggedInUser object
Dim LoggedInUser As New LoggedInUser
SQLStmt = String.Format("SELECT Id, Bailiwicks, LastName, FirstName FROM Accounts WHERE LoginId='{0}' and Password='{1}'", txtLoginId.Text, txtPassword.Text)
Cnxn.Open()
Dim BailiwickCommand As New OleDbCommand(SQLStmt, Cnxn)
Dim AccountData As OleDbDataReader = BailiwickCommand.ExecuteReader
If Not AccountData.Read() Then
MsgBox("Unable to read your account data from the database at this time...", MsgBoxStyle.Critical)
End
End If
LoggedInUser.AccountNumber = AccountData.Item("Id")
LoggedInUser.Bailiwick = AccountData.Item("Bailiwicks")
LoggedInUser.WebPageName = AccountData.Item("FirstName") _
& " " & AccountData.Item("LastName")
Cnxn.Close()
'Setup new MainPanel and send them there
Dim MainPanel As frmMainPanel
MainPanel = New frmMainPanel
MainPanel.Show()
Me.Hide()
End Sub