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

I am trying to read a single record from a table, capture the value from a single field, add 1 to the value and then write the value back out to the table.  Can someone tell me why I keep getting this error message "The Connection was not closed.  The Connection's current state is open.  I thought I am using completely different connections to do the read and then do the write.  

Here is my code:

----------------------------------------------------------------

//Read Record ID Counter


            decimal id;
            string taxid;
            string userid;
            string username;

            SqlCommand oCmd;
            SqlConnection oConn;
            SqlDataReader reader = null;
           

            oConn = new SqlConnection();
            oConn.ConnectionString = ConfigurationManager.ConnectionStrings["12123123ConnectionString"].ConnectionString;
       
            oConn.Open();
            //use stored proc name here
            oCmd = new SqlCommand("ReadDRRIdCntr", oConn);
            oCmd.CommandType = CommandType.StoredProcedure;

            // Get the data
            reader = oCmd.ExecuteReader();
                   
            reader.Read();
   
            if (reader.HasRows)
            {
                id = Convert.ToDecimal(reader["id"].ToString());

                id += (decimal)1;
                Session.Add("id", id);
            }

            if (oConn != null)
            {
                oConn.Close();
            }

            ///  ---------------   Write Updated Counter Back to Database
     
            SqlConnection UpdateNewPHICntrConn;
            UpdateNewPHICntrConn = new SqlConnection();
            UpdateNewPHICntrConn.ConnectionString = ConfigurationManager.ConnectionStrings["123123123ConnectionString"].ConnectionString;
            SqlCommand Updcmd;

            UpdateNewPHICntrConn.Open();
            Updcmd = new SqlCommand("UpdatePHICounter", oConn);
            Updcmd.CommandType = CommandType.StoredProcedure;
            id = Convert.ToDecimal(Session.Contents["id"].ToString());
            Updcmd.Parameters.Add("@ID", SqlDbType.Decimal).Value = id;
 
            Updcmd.Parameters.Clear();

            UpdateNewPHICntrConn.Open();

            // Write Updated Counter Data
            Updcmd.ExecuteNonQuery();

            if (UpdateNewPHICntrConn != null)
            {
                UpdateNewPHICntrConn.Close();
            }
kwh3856OwnerAsked:
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.

kwh3856OwnerAuthor Commented:
Sorry, forgot to add where the error occurs.

UpdateNewPHICntrConn.Open();---------------------------------------Error occurs on this line
0
appariCommented:
you are calling UpdateNewPHICntrConn.Open(); twice in your code.
remove/commentout  one line

///  ---------------   Write Updated Counter Back to Database
     
            SqlConnection UpdateNewPHICntrConn;
            UpdateNewPHICntrConn = new SqlConnection();
            UpdateNewPHICntrConn.ConnectionString = ConfigurationManager.ConnectionStrings["123123123ConnectionString"].ConnectionString;
            SqlCommand Updcmd;

            UpdateNewPHICntrConn.Open(); ////////****************** First time
            Updcmd = new SqlCommand("UpdatePHICounter", oConn);
            Updcmd.CommandType = CommandType.StoredProcedure;
            id = Convert.ToDecimal(Session.Contents["id"].ToString());
            Updcmd.Parameters.Add("@ID", SqlDbType.Decimal).Value = id;
 
            Updcmd.Parameters.Clear();

            UpdateNewPHICntrConn.Open(); ////////****************** Second time


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
SameerJagdaleCommented:
couple of things:
i prefer you write a code in try..catch..finally block. and close all your connections in finally. this will ensure that even though you have an exception, your connection will be closed.
further,
i suggest:
instead of directly opening the connection, use the code like one mentioned below:

if (oConn.State != ConnectionState.Open)
oConn.Open();

if (oConn != null && oConn.State != ConnectionState.Closed )
oConn.Close();
one important thing:
it looks like you are opening the connection twice.

UpdateNewPHICntrConn.Open(); //first time
Updcmd = new SqlCommand("UpdatePHICounter", oConn);
Updcmd.CommandType = CommandType.StoredProcedure;
id = Convert.ToDecimal(Session.Contents["id"].ToString());
Updcmd.Parameters.Add("@ID", SqlDbType.Decimal).Value = id;
Updcmd.Parameters.Clear();
UpdateNewPHICntrConn.Open(); // this is the second time
0
kwh3856OwnerAuthor Commented:
Thank you.  It is pretty late where I am and I guess my eyes are going cross on me:)

Thanks
Kenny
0
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
.NET Programming

From novice to tech pro — start learning today.