The connection was not closed. The connection's current state is open

Hi all,

I am trying to execute two commands and i received this error that says that " The connection was not closed. The connection's current state is open".  

Here is what i have.
    try
                {
                    myConnection.Open();
                    if ((int)cmd1.ExecuteScalar() == 1)
                    {
                        //update function
                        Bind_Data();
                        cmd0.ExecuteNonQuery();
                        txtTexbox1.Text = "Record Updated";
                    }
                    else
                    {
                        cmd.ExecuteNonQuery();

                        txtTexbox1.Text = "Saved";

                    }
               

                }
                catch (Exception exc)
                {
                    txtTexbox1.Text = exc.Message;
                }
                finally
                {
                    myConnection.Close();

                }


ANy suggestion?
shpresaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherCommented:
try
                {
                    if(myConnection.State == ConnectionState.Closed)
{
                    myConnection.Open();
}

Open in new window


finally
                {
                     if(myConnection.State == ConnectionState.Open || myConnection.State == ConnectionState.Broken)
                      myConnection.Close();

                }

Open in new window

0
anarki_jimbelSenior DeveloperCommented:
Just to add:


I believe the error happens because of the following line:

myConnection.Open();

Open in new window


Check if connection is already opened.
And it's up to you - you may close and open it again. Or use it as it is :).

One more thought. Looks like the connection was opened somewhere before your code is executed. Probably it had to be closed but wasn't. I'd find the place and close there...
0
Jacques Bourgeois (James Burger)PresidentCommented:
BindData probably contains code that calls Open on the same Connection object. If so, because the connection is already opened, you will get an error in BindData.

Make sure that it is opened in only one place, or close it after your ExecuteScalar.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.