Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Imports System.Data.OleDb
Public Class Form1

    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

         provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\Users\admin\Documents\database3.accdb"
        ' Dim connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Admin\Documents\Visual Studio 2010\Projects\WindowsApplication4\WindowsApplication4\Database3.accdb;USER ID=ADMIN"
        ' provider = "Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Admin\Documents\Visual Studio 2010\Projects\WindowsApplication4\WindowsApplication4\Database3.accdb;USER ID=ADMIN"
        connString = provider & dataFile
        ' myConnection.ConnectionString = connString
        ' myConnection.ConnectionString = connStr & dataFile
        ' the query:


        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [user1.database3.accdb] WHERE [username] = '" & TextBox1.Text & "' AND [password] = '" & TextBox2.Text & "'", myConnection)
        ' Dim dr As OleDbDataReader = cmd.ExecuteReader
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        ' the following variable is hold true if user is found, and false if user is not found
        Dim userFound As Boolean = False
        ' the following variables will hold the user first and last name if found.
        Dim FirstName As String = ""
        Dim LastName As String = ""

        'if found:
        While dr.Read
            userFound = True
            FirstName = dr("FirstName").ToString
            LastName = dr("LastName").ToString
        End While

        'checking the result
        If userFound = True Then
            ' Form2.Show()
            ' Form2.Label1.Text = "Welcome " & FirstName & " " & LastName
            MsgBox(" Welcome " & FirstName & " " & LastName)
        Else
            MsgBox("Sorry, username or password not found", MsgBoxStyle.OkOnly, "Invalid Login")
        End If
    End Sub


    Private Sub EXIT_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub

End Class


What I have tried:

i am trying to open this database but evry time ise gaves same reson as
ExecuteReader requires an open and available Connection. The connection's current state is closed.
pleae help to open this data
Posted
Updated 28-Mar-17 22:06pm
Comments
Richard MacCutchan 29-Mar-17 4:14am    
Given your previous question, and the above, you need to do some serious study on how to use databases in .NET.

Also you need to learn how to hash passwords in order to protect your customers' data.

You need to tell the myConnection object what its connection string is and you also need to explicitly open it using .Open.

Don't use a global connection like you are doing though, it will only lead to bugs and confusion as your code as no idea what the state of the connection is at any time. Define and create a new connection every time you need one.

You can see how to properly use a connection and ExecuteReader in the documentation

SqlCommand.ExecuteReader Method (System.Data.SqlClient)[^]
 
Share this answer
 
The error message is pretty explicit:
ExecuteReader requires an open and available Connection. The connection's current state is closed.
That is saying - very clearly - that myConnection is not Open - so all you need to do is call Open on myConnection at the top of the method, and Close when you are finished with it.

But...it would be a much, much better idea to build a new OleDbConnection specifically for this method to use, Open it, Close it, and Dispose it all within the method. Sharing a connection can give a lot of problems later on when you try to use it again from a nested method and it complains that a command is already in use. A Using block is teh simplest way to do this: Using Statement (Visual Basic)[^]

And two other very important things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This kind of code in a login is just asking for trouble!
2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but it's pretty simple and easy to understand.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900