I had face dificult problem. So I'm hoping your help. First I would like to discribe my form. Then You can understand my problem. This form had Comboboxes and textboxes and datetime pickers. We need to focus only few ones. So I'll type my form items below accendently.

  1. ComboBox - cmbloanid
  2. ComboBox - cmbcustomername
  3. TextBox - txtcustomerid
    cmbloanid combobox load itself loan id's when form load. Like that cmbcustomername combobox also load their values.
    And When I click on cmbloanid's one of item. at once all other details load to other controls (textboxes, datetimepickers). From This point I had error.
    Like that when I click on cmbcusomer name it load his customer id to below textbox (txtcustomerid).
    The error is this.
    When I select customer name from cmbcusomer combobox it loads his customer id. That's fine. To do that I coded.
    When I select loan id from cmbloanid combobox it say that "Connection is already open". I know why this happen. Let me explain.
    ComboBox select index change event I coded special thing.

    If select index = 0 then
    new_customer.show() '(that form is adding new customer)
    else
    customer_id_Load() 'this is private sub
    end if

I coded cmbloanid combobox like when index change call private sub. this private sub read data belong to loan table and load it to controls.
I'll add more codes below :
cmbloanid combobox index change code :

 Try
            load_loan_details()
 Catch ex As Exception
            XtraMessageBox.Show(ex.Message)
 End Try

cmbcustomername combobox index change code :

Try
            If ComboBox1.SelectedIndex = 0 Then
                Dim new_c_add_e = New Add_New_Customer_For_Edit_Loan(Me)
                new_c_add_e.Show()
                new_c_add_e.MdiParent = frmmlsadmin
            Else
                customer_id_load()
            End If
 Catch ex As Exception
            XtraMessageBox.Show(ex.Message)
 End Try

Firstly we faced cmbloanid and because of that firstly call first sub. Before it finish when customer name load according to loan id automatically call 2nd sub. I think that's why this " Connection already Open" error shows and interupt procedure.
I'm waiting for your help.

This means that you are trying to open a opened database connection. So Close the connection before open it if it is already opened.

'close your connection before opening it
If con.Status=Opened Then con.Close()

'now open it
con.Open()

where should I close connection ? I closed when it need inside the private sub.

This is best way to close an active connection at the end of all and every procedures after doing all database related works.

The code that @Shark_1 gave you, should go to your database connction function or what ever you have put your database connection at. Make sure that everytime when you open a connection, once done with it close it so that you can use it later or simply have a connection for each part instead of using one connection. That can be achieved by diclaring different databasr connection variables which each will be used on each related database connection part, But if your project doesn't require many connections using one connection and ensuring that you close it when you are not using it or before calling another Open command to prevent your error.

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.