I been working on this login page for months, and to no avail can have the program working. I am using the MSAccess Database and using VB.Net. This is a windows application creating a login page. The following is the code that I been working on:
Dim mypath = Application.StartupPath & "\password.mdb"
Dim Password = ""
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Documents and Settings\Owner\My Documents\OLEDB\password.mdb")
Dim cmd As OleDbCommand

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim sql = "SELECT Username,Password FROM Password WHERE Username = '" & txtUsername.Text & "' AND Password = '" & txtPassword.Text & "'"

cmd = New OleDbCommand(sql, conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader


Try
conn.Open()

Catch ex As InvalidOperationException
MsgBox(ex.Message)

End Try
Try
If dr.Read = False Then
MessageBox.Show("Authentication Failed...")
Else
MessageBox.Show("Login Successful...")

End If
Catch ex As Exception
MsgBox(ex.Message)

End Try

If conn.State <> ConnectionState.Closed Then
conn.Close()

End If


Dim form As New Form2
form.Show()

End Sub

The above code generates an error that points to the cmd.ExecuteReader
It says that the error : "ExecuteReader requires an open connection and available connection. The connection's current state is closed."
How can I use the open state for the ExecuteReader?

Dani AI

Generated

A short, practical addendum that ties the replies together and covers the follow-ups from , , and .

The original InvalidOperationException occurred because the reader was created before the connection was opened; the connection must be Open() before calling ExecuteReader (and it is good practice to scope connections so they get closed even on errors). See the language for the using pattern and connection handling for ADO.NET. (learn.microsoft.com)

Access-specific tips and a safe pattern to read extra fields (full name, account type): avoid using names that clash with Access reserved words (for example PASSWORD appears in Access reserved-word lists) or wrap identifiers in square brackets if renaming is not immediately possible. Use parameterized commands with ? placeholders when talking to Jet/OleDb and add parameters in the same order as the placeholders. (learn.microsoft.com)

Example pattern (VB.NET, simplified):

Using cn As New OleDbConnection(connectionString)
    cn.Open()
    Using cmd As New OleDbCommand("SELECT FullName, AccountType FROM Users WHERE Username = ? AND PasswordHash = ?", cn)
        cmd.Parameters.AddWithValue("p1", username)
        cmd.Parameters.AddWithValue("p2", passwordHash)
        Using rdr As OleDbDataReader = cmd.ExecuteReader()
            If rdr.Read() Then
                Dim fullName = If(rdr.IsDBNull(0), "", rdr.GetString(0))
                Dim acctType = If(rdr.IsDBNull(1), "", rdr.GetString(1))
                ' use fullName / acctType for welcome message
            End If
        End Using
    End Using
End Using

Security note: avoid storing plaintext passwords — store salted hashes (PBKDF2 / Rfc2898DeriveBytes or other modern KDFs) and verify hashes on login. For production, consider moving from an Access file to a server DB (SQL Server) if multiple users or stronger concurrency/security are required.

Recommended Answers

All 14 Replies

OK well the error is telling you exactly what is wrong. ExecuteReader requires an open connection and available connection

You need to add this line of code before you ExecuteReader. conn.Open()


Hope this helps

OK well the error is telling you exactly what is wrong. ExecuteReader requires an open connection and available connection

You need to add this line of code before you ExecuteReader. conn.Open()


Hope this helps

I really did because I tried it, and was missing in my coding. It is fixed and running good. I just need to be caredful how i code. The error message really drove me crazy, and thanks to you developers that assisted me in correcting my code. I will practice more. Now I am trying to learn the Stored Procedures.
Find it difficult, but will continue on it.

Glad it worked

If you have troubles with Store Procedures or anything in SQL/Access just post on the forums, someone will be glad to help!

I really did because I tried it, and was missing in my coding. It is fixed and running good. I just need to be caredful how i code. The error message really drove me crazy, and thanks to you developers that assisted me in correcting my code. I will practice more. Now I am trying to learn the Stored Procedures.
Find it difficult, but will continue on it.

well i tried n it didn work.. wher shud i write tht code line??

This codes works. I also need to fetch the user's full name and the account type into a variable, in order to be able to welcome the user by his full name. How do i do that? Thanx

how do i change background image on a command button at a runtime? Thanx

i guess you can do that in the form_load event.

Call the button in the load event

button1.image = (the path where you have the image of the button or place it in the bin)

I have a form (frmLogin) on which i have 4 command button n 1 is btnLogIn. so need to change the image on the btnLogIn after the user has logged In.

i had wanted to use something like imageList. Plz help
Thanx

[i have problem with that code could u plz send me the correct code to my e-mail id that is <snipped email> i will be very thankfull of you

place the

conn.Open() under the Try - Catch.. it will work

ok, well i no longer have the problem you guys have had. I don;t no what is wronge so ill ask you. ok the line

Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader

it apperes with the error "Syntax error in FROM clause"

Anyhelp were im going wronge?

Thanks

Hello every one,

i have a database in SQL Server 2008 with some login--> User
how to connect to the database via registered Login or User from a VN.net application
how to creat a login form for it to access sql server registered logins--> user

thank you in advance

commented: search the forum before posting. this has been answered multiple times -1

open connection befor declaring the ExecuteReader
like this
Dim sql = "SELECT Username,password FROM Login WHERE Username = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "'"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(sql, con)

Dim dr As OleDbDataReader = cmd.ExecuteReader

commented: you are only 4 years late. better reply to more recent threads. -3
Heading Here
Sub-Heading Here
  1. Item One
  2. Item Two
  3. Item Three
  • Item One
  • Item Two
  • Item Three

    Code blocks are created by indenting at least 4 spaces
    ... and can span multiple lines

    Inline Code Example Here Link Anchor Text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.