i make the vb homework
first, connect the mdb file, then login.
In the mdb file, there have login account and password.
when i run the program, it have some problem:
"dataAdapter.Fill(dt)" highlighted, The 'Microsoft.Jet.OLEDB.4.0xxxxxxx.mdb' provider is not registered on the local machine.
Private Sub loginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loginButton.Click
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0" & "Data Source=xxxxxxx.mdb"
Dim sqlStr As String = "Select * from account"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
For i As Integer = 0 To (dt.Rows.Count - 1)
If TextBox1.Text = CStr(dt.Rows(i)("accountid")) And TextBox2.Text = CStr(dt.Rows(i)("password")) Then
login = True
End If
Next
If login = True Then
MsgBox("logined")
Else
MsgBox("Incorrect username or password. Please try again.")
TextBox1.Clear()
TextBox2.Clear()
End If
You're missing a semi-colon (;) in your connection string.
Related
I am trying to set-up a login page that checks the user's credentials from an MS Access database. When I run the code, it says that the fill method needs one or more parameters. Here's the code I'm using:
Public Class login
Public Shared dslogin As New DataSet
Public Shared con As New OleDb.OleDbConnection
Dim da1 As New OleDb.OleDbDataAdapter
'Global variables
Public Shared currentloginID As Integer = 0
Public Shared tbusername As TextBox
Public Shared tbpassword As TextBox
Private Function OpenDBConnection1()
Dim directory1 As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Return "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = " & directory1 & "\IEshiDBv1.accdb"
End Function
'login button
Private Sub btnLOGIN_Click(sender As Object, e As EventArgs) Handles btnLOGIN.Click
Dim username As String = txtusername.Text
Dim password As String = txtpassword.Text
If username = "" Or password = "" Then
MsgBox("Please enter log-in details")
con.Close()
Exit Sub
Else
Dim sqllogin As String = ("SELECT * FROM LoginRcrds WHERE LoginUserName = " & username)
da1 = New OleDb.OleDbDataAdapter(sqllogin, con)
da1.Fill(dslogin, "LoginCredentials")
con.Close()
Dim dtlogin As DataTable = dslogin.Tables("LoginCredentials")
Dim rowlogin As DataRow
For Each rowlogin In dtlogin.Rows
If password = rowlogin("LoginPW") Then
homepage.Show()
Me.Hide()
Exit For
End If
Next
MsgBox("Incorrect log-in credentials. Please try again")
Exit Sub
End If
End Sub
'Onload subroutine
Private Sub login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = (OpenDBConnection1())
con.Open()
End Sub
End Class
The error occurs at
da1.Fill(dslogin, "LoginCredentials")
and the error message says " No value given for one or more required parameters."
I read somewhere that .Fill requires three parameters: (DataSet, RecordSet, TableName). I never tried this because I don't understand what RecordSet means.
Edit: I've checked another program with a 4.0 Oledb provider and changed it to 12.0. The same error appeared. It seems that the Fill method of the DataAdapter of Oledb 12.0 requires more than 2 parameters. Can somebody explain this?
I'm sorry for disturbing you guys, but I had a question to ask. I'm currently doing a program where user which are in the access database can log in, the code is working but the problem is that when I debug I can only login using 1 user, when I try logging in using another user account it shows Login Invalid and I'm not sure why. I hope someone could pin point what am I doing wrong;
Here's my code;
Imports System.Data.OleDb 'provides classes to connect to the database
Imports System.Data
Imports System.IO
Public Class Login
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Function getcount() As Integer
Using conn As New OleDb.OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\User.mdb")
'provider to be used when working with access database
conn.Open()
Dim cmd As New OleDb.OleDbCommand("Select COUNT(*) FROM UserProf_table", conn)
Return cmd.ExecuteScalar()
End Using
End Function
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\User.mdb"
conn.Open()
If getcount() = 1 Then
btnReg.Visible = False
Else
btnReg.Visible = True
End If
MsgBox(conn.State.ToString()) 'to check connection
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim idbx As String ' noted that ID is numbers
Dim pwd As String
idbx = idbox.Text
pwd = pwdbox.Text
With cmd
'Open Connection for executereader
If Not conn.State = ConnectionState.Open Then
conn.Open()
End If
'initialized database connection
.Connection = conn
.CommandText = "SELECT UserID, UserPwd FROM UserProf_table WHERE UserID = '" & idbox.Text & "' AND UserPwd = '" & pwdbox.Text & "'"
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
If idbx = dr.Item("UserID") And pwd = dr.Item("UserPwd") Then
idbx = SystemInformation.UserName
mainForm.Show()
Me.Hide()
Else
MsgBox("Password or username is incorrect")
idbox.Clear()
pwdbox.Clear()
End If
dr.Close()
End If
End With
'close connection
conn.Close()
End Sub
Private Sub btnReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReg.Click
registerForm.Show() ' Register form
Me.Hide()
End Sub
End Class
Here's my database:
I login using the UserID and UserPwd. And is there a way for me to save the UserID do that I can use it in different form? Thank you in advance
You are always reading the whole username/password list!
You have forgotten a WHERE clause in :
SELECT UserID, UserPwd FROM UserProf_table
WHERE UserId = ???
You can login as the first user because it's the first row returned!
[Also: please don't store passwords as plain text]
You should change your sql command to retrieve only 1 record:
.CommandText = "SELECT UserID,UserPwd FROM UserProf_table WHERE UserId =" + idbx.trim()
Moreover, if you are using Visual Studio, you can use "Watch and QuickWatch Windows" in debug mode to show your variables and make sure they return expected values.
https://msdn.microsoft.com/en-us/library/0taedcee.aspx
I am building this application within Vb 2008 that will allow users to login to their account (after having registered first) and view their information which has been stored in a database in MS Access. So far I have created the login and registration form, and the view profile form, but I want to know how on the users information from the data. I know that i will need to somehow send the id value of the user that has logged in from the login from to the view profile form,but how would I go about doing that.
Please if you could help I would really appreciate it
This is the code for my login form :
Imports System.Data.OleDb
Public Class Login
Dim ObjConnection As OleDbConnection
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim obj As New Profile
obj.StringPass = "SELECT ID FROM Members WHERE username='" & txtUsername.Text & "' "
' Check if username or password is empty
If txtPassword.Text = "" Or txtUsername.Text = "" Then
MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' Both fields was supply
' Check if user exist in database
' Connect to DB
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\My_db.accdb"
'conn.Open()
'MsgBox("Susscess")
Dim sql As String = "SELECT * FROM Members WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "'"
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
'Open Database Connection
sqlCom.Connection = conn
conn.Open()
Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
If sqlRead.Read() Then
Home.Show()
Me.Hide()
Else
' If user enter wrong username and password combination
' Throw an error message
MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'Clear all fields
txtPassword.Text = ""
txtUsername.Text = ""
'Focus on Username field
txtUsername.Focus()
End If
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'User clicking on cancel button only clears field
' and refocus to first field
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Register.Show()
End Sub
End Class
This is code for my register for
Imports System.Data.OleDb
Public Class Register
Dim ObjConnection As OleDbConnection
Private Sub MembersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MembersBindingNavigatorSaveItem.Click
Me.Validate()
Me.MembersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.My_dbDataSet)
End Sub
Private Sub Register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ObjConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\My_db.accdb")
ObjConnection.Open()
End Sub
Private Sub btnRegsiter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegsiter.Click
If txtForename.Text = "" Or txtSurname.Text = "" Or txtDate.Text = "" Or txtHeight.Text = "" Or txtWeight.Text = "" Or txtUsernameRegister.Text = "" Or txtPasswordRegister.Text = "" Then
MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If txtRe_type.Text = txtPasswordRegister.Text Then
Dim StrSQL As String = "INSERT INTO Members ([Forename], [Surname], [Date Of Birth], [Height], [Weigth], [Address] ,[Email], [Telephone No], [Username], [Password] ) VALUES (#ParForename, #ParSurname, #ParDate, #ParHeight, #ParWeigth, #ParAddress, #ParEmail, #ParTelephone, #ParUsername, #ParPassword)"
Dim ObjCommand As New OleDbCommand(StrSQL, ObjConnection)
ObjCommand.CommandType = CommandType.Text
ObjCommand.Parameters.Add("#ParForename", OleDbType.Char).Value = txtForename.Text
ObjCommand.Parameters.Add("#ParSurname", OleDbType.Char).Value = txtSurname.Text()
ObjCommand.Parameters.Add("#ParDate", OleDbType.Char).Value = txtDate.Text
ObjCommand.Parameters.Add("#ParHeight", OleDbType.Char).Value = txtHeight.Text
ObjCommand.Parameters.Add("#ParWeigth", OleDbType.Char).Value = txtWeight.Text
ObjCommand.Parameters.Add("#ParAddress", OleDbType.Char).Value = txtAddress.Text
ObjCommand.Parameters.Add("#ParEmail", OleDbType.Char).Value = txtEmail.Text
ObjCommand.Parameters.Add("#ParTelephone", OleDbType.Char).Value = txtTelephone.Text
ObjCommand.Parameters.Add("#ParUsername", OleDbType.Char).Value = txtUsernameRegister.Text
ObjCommand.Parameters.Add("#ParPassword", OleDbType.Char).Value = txtPasswordRegister.Text
ObjCommand.ExecuteNonQuery()
MsgBox("Registration Succesful")
Login.Show()
Me.Hide()
Else
MessageBox.Show("Your passwords do not match, please try again", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Login.Show()
Me.Hide()
End Sub
End Class
There are a lot of security concerns in your code that really should be addressed, but that notwithstanding, you just need to read the user id as part of the login procedure.
Currently you are just assuming validation on the fact that there is something to read, If sqlRead.Read() Then, rather than actually reading and storing a value (the user id) from the result set.
If you modify your SQL to, for example, SELECT user_id from .... and instead you made a call to sqlCom.ExecuteScalar() the return value would be the user_id which you could then store say in a session variable or some other persistent storage so that the id is available on the profile page.
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
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.
I'm new to VB.net. So here is what I want to do which I am having some serious problem
Using the "Get_Computer_Name form" will post the username and account type into another form. The Get_Computer_Name also gets the computer name where it will be sent to a module and that module will work as the main connection. Now I have tried to separate the login form and form that gets the computer name but that didn't work (I also want to try that method).
The problem with my current method is that it can only perform one thing at a time. It fails to post the username and account type and it fails to send the computer name to the module. Now strangely there is a way around. If I comment out the post username and account type, the computer name will be sent to the module and the module will work and connect to the database. Now if I choose not to comment out the code that post the username and account to the other form, the computer name won't be sent to the module that handles the connection thus failing to connect to the database.
I am open to other suggestions rather than doing it this way.
Thank you
'Here is my code for "Get_Computer_Name form"
Imports System.Data.SqlClient
Public Class Get_Computer_Name
Public Sub Get_Computer_Name_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
cmbcomputername.Text = My.Computer.Name
connect()
End Sub
Private Sub cmbcomputername_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbcomputername.SelectedIndexChanged, cmbcomputername.TextChanged
cmbcomputername.Text = My.Computer.Name
End Sub
Public Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
datasource = cmbcomputername.Text
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
Dim reader As SqlDataReader
Dim sqlstatement As SqlCommand = New SqlCommand
sqlstatement.Connection = connection
Try
sqlstatement.CommandText = "Select account_username, account_password, account_type " &
"from account_login where account_username='" & txtusername.Text & "' and account_password='" & txtpassword.Text & "'"
reader = sqlstatement.ExecuteReader()
If (reader.Read()) Then
Dim application_form As application_form
application_form = New application_form
application_form.Show()
application_form = Nothing
Me.Visible = False
'This is the code that post the username and account type to the other form (application form). If I comment this code out, the system successfully connects to the database
application_form.lblusername.Text = (reader("account_username").ToString)
application_form.lblaccount_type.Text = (reader("account_type").ToString)
sqlstatement.Dispose()
reader.Close()
connection.Close()
Else
connection.Close()
MsgBox("Invalid")
End If
reader.Close()
Catch ex As Exception
End Try
End Sub
End Class
'This is the code for the module that holds the main connection string
Public connection As SqlConnection = New SqlConnection
Public datasource As String
Public database As String = "bpmi"
Public sqlmainconnector As String
Public Sub connect()
sqlmainconnector = "Data Source=" & datasource & ";Initial Catalog=bpmi;Integrated Security=True"
connection.ConnectionString = sqlmainconnector
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
Else
connection.Close()
End If
Catch ex As Exception
End Try
End Sub
'And this code is the form where the username and account type will be posted
Imports System.Data.SqlClient
Public Class application_form
Dim sqlcommand As SqlCommand
Dim da As SqlDataAdapter
Dim table As New DataTable
Dim dategrabberapp As String
Dim dategrabberben As String
Dim benidgrabber As String
Private Sub Form1_Load(sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connect()
filldatagrid(updateappdatagrid, updatebendatagrid)
fillcomboboxstatus(cmbstatus)
fillcomboboxposition(cmbappworkposition)
fillcomboboxrelationship(cmbrelation)
fillcomboboxrelationshipupdate(cmbrelationbenupdate)
fillcomboboxstatusupdate(cmbappstatusupdate)
fillcomboboxpositionupdate(cmbappworkposupdate)
'this is where the account type will got to
If lblaccount_type.Text <> "Administrator" Then
Me.TabControl1.TabPages(1).Enabled = False
Me.TabControl1.TabPages(2).Enabled = False
MsgBox("As a 'Staff', you are not permitted to do any updates")
updateappdatagrid.Hide()
updatebendatagrid.Hide()
End If
End Sub
End Class