Hi All,
I have been using Oledb to update my access file with user name and password.
I have created an accesss file with a pre defined table (Only Username and password fields) and added it to the project using add existing item.
My intention is to create dll file which can be used widely for creating a user log in mechanism for my form projects.
Everything works fine till inserting records in the table in access. the user names stored in the access file are then linked to a combo box to facilitate the user when he wants to log in. This is where the problem starts . the entire contents of the table are deleted and the combo box turns up empty. Dont know whats going on wrong.
i use Windows7 -64 bit system, VS 2010, Access 2007 32 bit. Following is the class i have made in vb.net.
Thank You in advance
Regards
Rameshwar Prasad
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class LogIn_User
Private New_User_datareader As OleDb.OleDbDataReader
Private Newusername As String
Private userpassword As String
Private Confirm_userpassword As String
Private Access_File_Name As String
Private Newuser_Connectionstring As String
Private Newuseroledbconn1 As New OleDbConnection
Private Newusercommand1_string As String
Private Newusercommand2_string As String
Private Newusercommand1 As New OleDbCommand
Private Newusercommand2 As New OleDbCommand
Private useresp As Integer
'Variables for Existing User
Private Existing_User_Connection_String As String
Private Existing_User_Connection1 As New OleDbConnection
Private Existing_User_Command1_string As String
Private Existing_User_DataAdapter As New OleDbDataAdapter
'Dataset represents an in memory cache of data
Private Existing_User_Dataset As New DataSet
'DataTable represents one table of in-memory data
Private Existing_User_Datatable As New DataTable
'Variable to record UserName selected to login to the system
Private LoggedInUserdrv As DataRowView
Private LoggedInUser As String
'Variable to record password entered by user
Private Actual_user_Password As String
Private User_Entered_Password As String
Private UE_Password_ConnectionString1 As String
Private UE_Password_String2 As String
Private UE_Password_Connection1 As OleDbConnection
Private UE_Password_Command1 As OleDbCommand
Private UE_Password_Command2 As OleDbCommand
Private UE_Password_Reader As OleDbDataReader
Public Sub Record_entered_values(ByVal formname As Form, ByVal TBusername As TextBox, ByVal TBuserpassword As TextBox, ByVal TBconfirmpassword As TextBox)
With formname
Newusername = TBusername.Text
userpassword = TBuserpassword.Text
Confirm_userpassword = TBconfirmpassword.Text
End With
End Sub
Public Sub Create_user(ByVal Previousformname As Form, ByVal formname As Form, ByVal Passwordaccessfilename As String, ByVal Accesstablename As String, ByVal column1_name As String, ByVal column2_password As String)
'First check whether the password enterd in both the textboxes match or not
If userpassword = Confirm_userpassword Then
Newuser_Connectionstring = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & Passwordaccessfilename
Newuseroledbconn1 = New OleDbConnection(Newuser_Connectionstring)
Newusercommand1_string = "SELECT " & column1_name & " FROM " & Accesstablename & " WHERE " & column1_name & " = '" & Newusername & "'"
Newusercommand1 = New OleDb.OleDbCommand(Newusercommand1_string, Newuseroledbconn1)
Newusercommand1.Connection = Newuseroledbconn1
Newuseroledbconn1.Open()
New_User_datareader = Newusercommand1.ExecuteReader
If Not New_User_datareader.HasRows Then
GoTo 1
Else
MessageBox.Show("User already exists, Enter unique username!", "Attention!")
Newuseroledbconn1.Close()
Exit Sub
End If
1: Newusercommand2_string = "INSERT INTO[" + Accesstablename + "] ([" + column1_name + "] ,[" + column2_password + "]" _
& ") VALUES(?,?)"
Newusercommand2 = New OleDb.OleDbCommand(Newusercommand2_string, Newuseroledbconn1)
Newusercommand2.Parameters.AddWithValue("@Username", Newusername)
Newusercommand2.Parameters.AddWithValue("@Password", userpassword)
Try
'execute command
'check duplicate newuser
Newusercommand2.ExecuteNonQuery()
MessageBox.Show("New User Successfully registered", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
useresp = MsgBox("Do You want to add another User?", vbYesNo, "Add more?")
If useresp = 7 Then
formname.Hide()
Previousformname.Show()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Newuseroledbconn1.Close()
End Try
ElseIf userpassword <> Confirm_userpassword Then
MessageBox.Show("Passwords do not match", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Newuseroledbconn1.Close()
Accesstablename = Nothing
Previousformname = Nothing
formname = Nothing
Passwordaccessfilename = Nothing
Accesstablename = Nothing
column1_name = Nothing
column2_password = Nothing
End Sub
'A sub to enable user to login to the application
Public Sub Existing_User_Log_in(ByVal Previousformname As Form, ByVal formname As Form, ByVal passwordAccessFilename As String, ByVal Accesstablename As String, ByVal column1_name As String, ByVal column2_password As String, ByVal CBBox As ComboBox)
'First create a connecion by providing information of dataprovider and datasource
Existing_User_Connection_String = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & passwordAccessFilename & ";"
Existing_User_Connection1 = New OleDbConnection(Existing_User_Connection_String)
Existing_User_Command1_string = "SELECT " & column1_name & " FROM " & Accesstablename
'Instantiate a data adapter
Existing_User_DataAdapter = New OleDbDataAdapter(Existing_User_Command1_string, Existing_User_Connection1)
'use the data adapter to fill the dataset with the table
Existing_User_DataAdapter.Fill(Existing_User_Dataset, Accesstablename)
Existing_User_Datatable = Existing_User_Dataset.Tables(Accesstablename)
With formname
CBBox.DataSource = Existing_User_Datatable
CBBox.DisplayMember = column1_name
End With
End Sub
Public Sub Record_Logged_In_user(ByVal cbbox As ComboBox, ByVal column1_name As String)
LoggedInUserdrv = cbbox.SelectedValue
LoggedInUser = LoggedInUserdrv.Item(column1_name).ToString
End Sub
Public Sub Check_Existing_User_Password(ByVal nextformname As Form, ByVal formname As Form, ByVal TBUserenteredPassword As TextBox, ByVal accessfilename As String, ByVal AccessTablename As String, ByVal column1_name As String, ByVal column2_name As String)
With formname
If TBUserenteredPassword.Text <> "" Then
User_Entered_Password = TBUserenteredPassword.Text
GoTo 1
Else
MessageBox.Show("Password Required", "Empty entry detected", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End With
1: UE_Password_ConnectionString1 = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & accessfilename & "; "
UE_Password_Connection1 = New OleDbConnection(UE_Password_ConnectionString1)
UE_Password_String2 = " SELECT " & column2_name & " FROM " & AccessTablename & " WHERE " & column1_name & " ='" & LoggedInUser & "'"
UE_Password_Command1 = New OleDbCommand(UE_Password_String2, UE_Password_Connection1)
UE_Password_Connection1.Open()
UE_Password_Reader = UE_Password_Command1.ExecuteReader
If UE_Password_Reader.HasRows Then
UE_Password_Reader.Read()
'this variable was used in entering the password in he access table while registering a new user. the same variable is used here
Actual_user_Password = UE_Password_Reader.Item(column2_name).ToString
If User_Entered_Password = userpassword Then
MessageBox.Show("correct passsword entered")
Else
MessageBox.Show("Error")
End If
UE_Password_Connection1.Close()
End If
End Sub
End Class
I have been using Oledb to update my access file with user name and password.
I have created an accesss file with a pre defined table (Only Username and password fields) and added it to the project using add existing item.
My intention is to create dll file which can be used widely for creating a user log in mechanism for my form projects.
Everything works fine till inserting records in the table in access. the user names stored in the access file are then linked to a combo box to facilitate the user when he wants to log in. This is where the problem starts . the entire contents of the table are deleted and the combo box turns up empty. Dont know whats going on wrong.
i use Windows7 -64 bit system, VS 2010, Access 2007 32 bit. Following is the class i have made in vb.net.
Thank You in advance
Regards
Rameshwar Prasad
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class LogIn_User
Private New_User_datareader As OleDb.OleDbDataReader
Private Newusername As String
Private userpassword As String
Private Confirm_userpassword As String
Private Access_File_Name As String
Private Newuser_Connectionstring As String
Private Newuseroledbconn1 As New OleDbConnection
Private Newusercommand1_string As String
Private Newusercommand2_string As String
Private Newusercommand1 As New OleDbCommand
Private Newusercommand2 As New OleDbCommand
Private useresp As Integer
'Variables for Existing User
Private Existing_User_Connection_String As String
Private Existing_User_Connection1 As New OleDbConnection
Private Existing_User_Command1_string As String
Private Existing_User_DataAdapter As New OleDbDataAdapter
'Dataset represents an in memory cache of data
Private Existing_User_Dataset As New DataSet
'DataTable represents one table of in-memory data
Private Existing_User_Datatable As New DataTable
'Variable to record UserName selected to login to the system
Private LoggedInUserdrv As DataRowView
Private LoggedInUser As String
'Variable to record password entered by user
Private Actual_user_Password As String
Private User_Entered_Password As String
Private UE_Password_ConnectionString1 As String
Private UE_Password_String2 As String
Private UE_Password_Connection1 As OleDbConnection
Private UE_Password_Command1 As OleDbCommand
Private UE_Password_Command2 As OleDbCommand
Private UE_Password_Reader As OleDbDataReader
Public Sub Record_entered_values(ByVal formname As Form, ByVal TBusername As TextBox, ByVal TBuserpassword As TextBox, ByVal TBconfirmpassword As TextBox)
With formname
Newusername = TBusername.Text
userpassword = TBuserpassword.Text
Confirm_userpassword = TBconfirmpassword.Text
End With
End Sub
Public Sub Create_user(ByVal Previousformname As Form, ByVal formname As Form, ByVal Passwordaccessfilename As String, ByVal Accesstablename As String, ByVal column1_name As String, ByVal column2_password As String)
'First check whether the password enterd in both the textboxes match or not
If userpassword = Confirm_userpassword Then
Newuser_Connectionstring = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & Passwordaccessfilename
Newuseroledbconn1 = New OleDbConnection(Newuser_Connectionstring)
Newusercommand1_string = "SELECT " & column1_name & " FROM " & Accesstablename & " WHERE " & column1_name & " = '" & Newusername & "'"
Newusercommand1 = New OleDb.OleDbCommand(Newusercommand1_string, Newuseroledbconn1)
Newusercommand1.Connection = Newuseroledbconn1
Newuseroledbconn1.Open()
New_User_datareader = Newusercommand1.ExecuteReader
If Not New_User_datareader.HasRows Then
GoTo 1
Else
MessageBox.Show("User already exists, Enter unique username!", "Attention!")
Newuseroledbconn1.Close()
Exit Sub
End If
1: Newusercommand2_string = "INSERT INTO[" + Accesstablename + "] ([" + column1_name + "] ,[" + column2_password + "]" _
& ") VALUES(?,?)"
Newusercommand2 = New OleDb.OleDbCommand(Newusercommand2_string, Newuseroledbconn1)
Newusercommand2.Parameters.AddWithValue("@Username", Newusername)
Newusercommand2.Parameters.AddWithValue("@Password", userpassword)
Try
'execute command
'check duplicate newuser
Newusercommand2.ExecuteNonQuery()
MessageBox.Show("New User Successfully registered", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
useresp = MsgBox("Do You want to add another User?", vbYesNo, "Add more?")
If useresp = 7 Then
formname.Hide()
Previousformname.Show()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Newuseroledbconn1.Close()
End Try
ElseIf userpassword <> Confirm_userpassword Then
MessageBox.Show("Passwords do not match", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Newuseroledbconn1.Close()
Accesstablename = Nothing
Previousformname = Nothing
formname = Nothing
Passwordaccessfilename = Nothing
Accesstablename = Nothing
column1_name = Nothing
column2_password = Nothing
End Sub
'A sub to enable user to login to the application
Public Sub Existing_User_Log_in(ByVal Previousformname As Form, ByVal formname As Form, ByVal passwordAccessFilename As String, ByVal Accesstablename As String, ByVal column1_name As String, ByVal column2_password As String, ByVal CBBox As ComboBox)
'First create a connecion by providing information of dataprovider and datasource
Existing_User_Connection_String = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & passwordAccessFilename & ";"
Existing_User_Connection1 = New OleDbConnection(Existing_User_Connection_String)
Existing_User_Command1_string = "SELECT " & column1_name & " FROM " & Accesstablename
'Instantiate a data adapter
Existing_User_DataAdapter = New OleDbDataAdapter(Existing_User_Command1_string, Existing_User_Connection1)
'use the data adapter to fill the dataset with the table
Existing_User_DataAdapter.Fill(Existing_User_Dataset, Accesstablename)
Existing_User_Datatable = Existing_User_Dataset.Tables(Accesstablename)
With formname
CBBox.DataSource = Existing_User_Datatable
CBBox.DisplayMember = column1_name
End With
End Sub
Public Sub Record_Logged_In_user(ByVal cbbox As ComboBox, ByVal column1_name As String)
LoggedInUserdrv = cbbox.SelectedValue
LoggedInUser = LoggedInUserdrv.Item(column1_name).ToString
End Sub
Public Sub Check_Existing_User_Password(ByVal nextformname As Form, ByVal formname As Form, ByVal TBUserenteredPassword As TextBox, ByVal accessfilename As String, ByVal AccessTablename As String, ByVal column1_name As String, ByVal column2_name As String)
With formname
If TBUserenteredPassword.Text <> "" Then
User_Entered_Password = TBUserenteredPassword.Text
GoTo 1
Else
MessageBox.Show("Password Required", "Empty entry detected", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End With
1: UE_Password_ConnectionString1 = "provider = microsoft.Ace.oledb.12.0; " & _
"data source =" & Application.StartupPath & "\" & accessfilename & "; "
UE_Password_Connection1 = New OleDbConnection(UE_Password_ConnectionString1)
UE_Password_String2 = " SELECT " & column2_name & " FROM " & AccessTablename & " WHERE " & column1_name & " ='" & LoggedInUser & "'"
UE_Password_Command1 = New OleDbCommand(UE_Password_String2, UE_Password_Connection1)
UE_Password_Connection1.Open()
UE_Password_Reader = UE_Password_Command1.ExecuteReader
If UE_Password_Reader.HasRows Then
UE_Password_Reader.Read()
'this variable was used in entering the password in he access table while registering a new user. the same variable is used here
Actual_user_Password = UE_Password_Reader.Item(column2_name).ToString
If User_Entered_Password = userpassword Then
MessageBox.Show("correct passsword entered")
Else
MessageBox.Show("Error")
End If
UE_Password_Connection1.Close()
End If
End Sub
End Class