error in connection for login form any help with the attached code

msaccess , ado.net , vb.net Southampton, United Kingdom
  • 10 years ago

    hi, i am new in vb.net in the login form if i enter an invalid username the next time it gives me this error "Not allowed to change the 'ConnectionString' property. The connection's current state is open."

    any help with the following code

    Imports System.Data Imports System.Data.OleDb Public Class LoginForm1 Dim DbCon As New OleDb.OleDbConnection Dim dbUp As New OleDb.OleDbCommand Dim Read As OleDb.OleDbDataReader

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    
        DbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\login.mdb"
        DbCon.Open()
        dbUp.Connection = DbCon
        dbUp.CommandType = CommandType.Text
        dbUp.CommandText = "Select * from LOGIN where Username= '" & UsernameTextBox.Text & "' and Password= '" & PasswordTextBox.Text & "'"
        dbUp.Parameters.Add("Username", Data.OleDb.OleDbType.Variant)
        dbUp.Parameters.Add("Password", Data.OleDb.OleDbType.Variant)
        dbUp.Parameters("Username").Value = UsernameTextBox.Text
        dbUp.Parameters("Password").Value = PasswordTextBox.Text
        Read = dbUp.ExecuteReader
        With Read
            If .Read Then
                Me.Hide()
                Form1.Show()
            Else
                UsernameTextBox.Clear()
                PasswordTextBox.Clear()
                MessageBox.Show("Invalid Username or Password")
                UsernameTextBox.Focus()
            End If
    
    
        End With
    
    End Sub
    
  • 10 years ago

    Try this source code of mind:

    Dim strQuery As String Dim cmd As OleDbCommand

    Dim sda As OleDbDataReader
    Dim con As OleDbConnection
    

    con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Files\Inventory\database\dbInventory.mdb;Persist Security Info=True;") con.Open()

    strQuery = "SELECT *FROM qryUsers_Group WHERE UserName='" & txtUserName.Text.Trim() & "' AND Password='" & FuncCls.EncryptPassword(txtPassword.Text.Trim()) & "'"

            cmd = New OleDbCommand(strQuery, con)
            sda = cmd.ExecuteReader
            If sda.Read() Then
                lblMessage.Text = "Password Successful."
    

    Else lblMessage.Text = "Invalid Password." End If

            sda.Close()
    

    if you want SQL i can give you, or even stored Procedure.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook