|
-
Apr 18th, 2013, 02:33 PM
#1
Thread Starter
New Member
[RESOLVED] Help with login form
Ok so basically I have a login form connected to a database and it (Database) contains my usernames and passwords. I also included a security level so that if a certain member logs on, features to them will be disabled. So For example if Katie logged in she can view all records BUT cannot delete them.
However there is a problem, each time I try to log in the following message appears:
"object reference not set to an instance of an object" either that message appears or this one:
"not allowed to change the 'connectionstring' property. the connection's current state is open."
I've been trying to solve this problem for a while now and I can't fix it.
Below is the code for my login form:
Code:
Imports System.Data.OleDb
Public Class frmLogin
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim sql As String
Dim dt As New DataTable
Dim DReader As OleDbDataReader
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
'Dim dr As DataRow
Dim dr As OleDbDataReader
'Dim dt As DataTable
Dim user As String = Trim(txtUsername.Text)
Dim pword As String = Trim(txtPassword.Text)
' If the username or password fields are empty then this displays an error message:
If user = "" Then
MessageBox.Show("Please enter the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf pword = "" Then
MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If user <> "" And pword <> "" Then
End If
dbSource = "Data Source =G:\Assignment 2 codes\ValleyMilk.accdb"
dbProvider = "PROVIDER = Microsoft.Ace.OLEDB.12.0;"
Try
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM Users WHERE Username= user AND [Password]= pword"
Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("Username", user)
cmd.Parameters.AddWithValue("Password", pword)
dr = cmd.ExecuteReader
' If dr.Read() = True And user = dr("Username").ToString And pword = dr("Password").ToString Then
'LogginUserLevel and LoggedinUser are from Module1.vb
LoggedinUserLevel = DReader("SecurityLevel")
LoggedinUser = DReader("Username")
If dr.Read() = True Then
MsgBox("Welcome back !")
Me.Hide()
frmMainMenu.Show()
Else
MsgBox("Login Failure")
dr.Close()
con.Close()
End If
Catch errObj As Exception
MessageBox.Show(errObj.Message)
End Try
Any help on this would be great.
-
Apr 18th, 2013, 03:06 PM
#2
Re: Help with login form
on what lines of code do these errors occur?
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 18th, 2013, 03:13 PM
#3
Thread Starter
New Member
Re: Help with login form
I suspect its this part:
Code:
LoggedinUserLevel = DReader("SecurityLevel")
LoggedinUser = DReader("Username")
If dr.Read() = True Then
MsgBox("Welcome back !")
Me.Hide()
frmMainMenu.Show()
Else
MsgBox("Login Failure")
dr.Close()
con.Close()
I removed the "Dreader" part and the login worked fine, but I need the securitylevel part to be in. Also the following variables:
LoggedinUserLevel = DReader("SecurityLevel")
LoggedinUser = DReader("Username")
Are in a module so they will be recognised everywhere.
If it helps I can send you the form and the database if you want.
-
Apr 18th, 2013, 03:19 PM
#4
Re: Help with login form
I'mm not well versed in oledb stuff, but when an object is not set to a reference, it means that even you may have created the variable, you never instance it.
Are you mistakenly using dr as the reader when you should be using DReader?
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 18th, 2013, 03:28 PM
#5
Thread Starter
New Member
Re: Help with login form
That's what I thought it was but the same messages still appear.
-
Apr 18th, 2013, 03:37 PM
#6
Re: Help with login form
it doesn't look like you are ever creating the Dreader object. You do this...
dr = cmd.ExecuteReader
and then are trying to use Dreader to get the value which are in dr. (Unless you are using Dreader elsewhere)
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 18th, 2013, 07:48 PM
#7
Lively Member
Re: Help with login form
Just a suggestion use try catch finally. You appear to only close connection if it was sucessful. Should close it no matter what happens to prevent memory leaks and other problems.
-
Apr 18th, 2013, 10:18 PM
#8
Re: Help with login form
 Originally Posted by Lewis194
I suspect its this part:
Code:
LoggedinUserLevel = DReader("SecurityLevel")
LoggedinUser = DReader("Username")
If dr.Read() = True Then
MsgBox("Welcome back !")
Me.Hide()
frmMainMenu.Show()
Else
MsgBox("Login Failure")
dr.Close()
con.Close()
I removed the "Dreader" part and the login worked fine, but I need the securitylevel part to be in. Also the following variables:
LoggedinUserLevel = DReader("SecurityLevel")
LoggedinUser = DReader("Username")
Are in a module so they will be recognised everywhere.
If it helps I can send you the form and the database if you want.
You don't have to suspect anything. The IDE will tell you exactly where the error occurred. You're catching the exception and displaying the message but the exception contains much more information than that. Look at it.
-
May 3rd, 2013, 04:43 AM
#9
Thread Starter
New Member
Re: Help with login form
Hi people sorry for the late response I've been a bit busy but I solved the problem, turns out my 'Dreader' was in the wrong place it should have been within the If statement below it. Anyways thank you all for replying to this thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|