hey , I'm at unhandled error. if you can give me help I apreciate it :)

when I run software it say "connection already open". I find all, but I can't find.

 If Len(Trim(txtloanids.Text)) < 4 Then
            MessageBox.Show("Please Check Custormer ID", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtloanids.Focus()
            Exit Sub
        End If
        query = "SELECT * FROM mcs.loan WHERE loan_id ='" & txtloanids.Text & "'"
        Dim cmd As MySqlCommand = New MySqlCommand(query, con)
        Dim dr As MySqlDataReader
        Try
            con.Open()
            dr = cmd.ExecuteReader()
            If (dr.HasRows) Then
                While (dr.Read())
                    Me.txtcustormeridls.Text = Convert.ToString(dr("custormer_id"))
                    Me.txtcustormernamels.Text = Convert.ToString(dr("custormer_name"))
                    Me.txtcreditamounts.Text = Convert.ToDecimal(dr("credit_amount"))
                    Me.txtinterestrates.Text = Convert.ToDecimal(dr("interest_rate"))
                    Me.txtperiods.Text = Convert.ToString(dr("period"))
                    Me.txtinterestforcredits.Text = Convert.ToDecimal(dr("interest_for_credit"))
                    Me.txttotalamounts.Text = Convert.ToString(dr("total_amount"))
                    Me.txtdailyinstallments.Text = Convert.ToString(dr("daily_installment"))
                    Me.txtloandates.Text = Convert.ToDateTime(dr("loan_date"))
                    Me.txttotalamountls.Text = Convert.ToString(dr("total_amount"))
                    Me.Label105.Text = Convert.ToString(dr("loan_status"))
                End While
                query = "SELECT sum(Amount) as sumam FROM mcs.income WHERE Loan_ID ='" & txtloanids.Text & "'"
                Dim cmd1 = New MySqlCommand(query, con)
                Dim dr1 As MySqlDataReader
                'Try
                con.Open()
                dr1 = cmd1.ExecuteReader()
                While dr1.Read()
                    Me.txtreceivedls.Text = Convert.ToDecimal(dr1("sumam"))
                End While
                dr1.Close()
                con.Close()
                'Catch ex As Exception
                ' MsgBox(ex.Message)
                MsgBox("This Laon Still Not Yet Received Any Settlment")
                Try
                    If (con.State = ConnectionState.Open) Then
                        con.Close()
                    End If
                Catch ex2 As Exception
                    MsgBox(ex2.Message)
                End Try
                'End Try
            Else
                MessageBox.Show("Search Loan With Another Loan ID", "This Loan Does Not Exist")
            End If
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Try
                If (con.State = ConnectionState.Open) Then
                    con.Close()
                End If
            Catch ex1 As Exception
                MsgBox(ex1.Message)
            End Try
        End Try
        txtarrearsls.Text = Val(txttotalamountls.Text) - Val(txtreceivedls.Text)

Recommended Answers

All 3 Replies

You could try a Finally clause which is executed whether or not an error occurs.

Try
    con.Open
    .
    .
    'don't do the con.Close here
Catch ex As Exception
    MsgBox(ex.Message)
    'don't do the con.Close here either
Finally
    If con.State = ConnectionState.Open Then
        con.Close()
    End If
End Try
commented: Good habit to close a connection object at the end of a proceedure/function. +7

Is con one and only one connection object in your project and already opened it at the time of starting of your application ? If it is, don't open it in every procedure or function because you already opened it at the time of your application has started and close it when you are trying to close your application. (Not recommanded)
If you declared a proceedure lavel connection object and already opened it, don't try to open it another time in that proceedure or if you want to open it further more try to close it first then open it and at the end of that proceedure always try to close it.

Reverend Jim has already give you a lesson how could you use it.

thank you both you sir :)
I had overcame from problem

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.