Hi, I am creating an login page connected to a database. I am using access ODB. the problem is, if the user makes an wrong input, and the invalid data message box appeared, when typing the data again, and press ok, the following exeption occured:
Not allowed to change the 'ConnectionString' property. The connection's current state is open.

here is the code.

much needed help, thank you.

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|\unpw.mdb"
        DbCon.Open()
        dbUp.Connection = DbCon
        dbUp.CommandType = CommandType.Text
        dbUp.CommandText = "Select * from UNPW 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
                Contacts.Show()
                Me.Close()


            Else
                UsernameTextBox.Clear()
                PasswordTextBox.Clear()
                MessageBox.Show("Invalid Username or Password")
                UsernameTextBox.Focus()

            End If
        End With
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        If MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        Else
        End If
    End Sub

Dani AI

Generated

You are seeing this because the same OleDbConnection is kept open between clicks and you try to set its ConnectionString again on the next attempt. Closing it manually (as suggested) can avoid the first error, but the follow-up RCW error usually happens when form-level connection/command/reader objects are reused after they were closed or disposed. The most reliable pattern is to scope all ADO.NET objects to the single operation and parameterize the query.

' Helper you can call from the OK button click
Private Function IsValidLogin(user As String, pass As String) As Boolean
    Const cs As String = "<your connection string here>"
    Using con As New OleDb.OleDbConnection(cs),
          cmd As New OleDb.OleDbCommand("SELECT COUNT(*) FROM [UNPW] WHERE [Username]=? AND [Password]=?", con)
        ' OleDb (Access) ignores parameter names; order must match the ? placeholders
        cmd.Parameters.Add("@u", OleDb.OleDbType.VarWChar, 255).Value = user
        cmd.Parameters.Add("@p", OleDb.OleDbType.VarWChar, 255).Value = pass
        con.Open()
        Return CInt(cmd.ExecuteScalar()) > 0
    End Using
End Function

Notes that address issues in your snippet:

  • Do not embed control names or values inside the SQL string. Use parameters only. For Access, wrap identifiers that might be reserved words, e.g., [Password].
  • Avoid storing DbConnection, OleDbCommand, or OleDbDataReader as class fields. Create them inside the method, and let Using handle disposal so you are never talking to a disposed COM object.
  • Keep the connection string constant (ideally in App.config) and do not reassign it on each click. Call the helper from your button and branch on the Boolean result.
  • Security: storing plaintext passwords is risky; prefer salted password hashes and compare hashes instead of raw text.

Recommended Answers

All 3 Replies

close the database connection before end sub of OK.

DbCon.Close()

{"COM object that has been separated from its underlying RCW cannot be used."}

Read = dbUp.ExecuteReader

i'm confused. it should work..

never mind. I made an work around. thank you.

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.