Search code examples
sqlinner-query

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


EDIT

After staring at this for 2 days, I do see one issue. I was still opening the original connection. So I changed the inner open statements to conn2.Open. Then, I changed the second inner query to where all the variables were number 3 instead of 2 so that they were completely different than the previous query. At that point, I got the error:

There is already an open DataReader associated with this Command which must be closed first.

I took out the inner connections, thinking I could use the outer connection and took out the inner .Close lines, but that also returned an error saying the connection was not closed.

END EDIT

I am writing a script that updates user information with data pulled from other tables where that user may be in it multiple times for purchases made.

So first, the "outside" sql query pulls some data from the items table which contains purchaser information as well as category information. For each item, it is going to check it's purchaser's information.

Second, the first "inner" sql query pulls category information from the user table. Some code is then run to see if they're already marked as purchasing from the category of the "outside" query. If they are not, it adds the category to a string variable.

Lastly, the second "inner" sql query updates the user table for the current user with the new category list.

I've asked about how to perform queries like this before, but was always given a solution of combining the queries into one. That worked for the other queries, but I cannot do that here. I must iterate through each record of the outer query to perform the necessary functions inside of it. But my issue here is that I get an SQL error saying that the connection was not closed, and it points to the catch of the outer query (for 'conn').

I had tried to set my 2 inner queries so that they used different connection variables (conn2 and conn3), and also different strSQL variables, but that didn't help. And I'm still a newb when it comes to SQL, having programmed using MySQL until this probject. Any help would be greately appreciated.

    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
    using (SqlCommand strSQL = conn.CreateCommand())
    {
        strSQL.CommandText = "SELECT field FROM itemsTable";
        try
        {
            conn.Open();
            using (SqlDataReader itemReader = strSQL.ExecuteReader())
            {
                while (itemReader.Read())
                {
                    {Do some stuff here}

                    using (SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
                    using (SqlCommand strSQL2 = conn2.CreateCommand())
                    {
                        strSQL2.CommandText = "SELECT fields FROM userTable";
                        try
                        {
                            conn2.Open();
                            using (SqlDataReader itemReader2 = strSQL2.ExecuteReader())
                            {
                                while (itemReader2.Read())
                                {
                                    {Do stuff here}
                                }
                                itemReader2.Close();
                            }
                        }
                        catch (Exception e3)
                        {
                            throw new Exception(e3.Message);
                        }
                        finally
                        {
                            conn2.Close();
                        }
                    }

                    {Do some more stuff here}

                    using (SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
                    using (SqlCommand strSQL2 = conn2.CreateCommand())
                    {
                        strSQL2.CommandText = "UPDATE userTable set field='value'";
                        try
                        {
                            conn2.Open();
                            strSQL2.ExecuteNonQuery();
                        }
                        catch (Exception e2)
                        {
                            throw new Exception(e2.Message);
                        }
                        finally
                        {
                            conn2.Close();
                        }
                    }

                    {Do even more stuff here.}
                }

                itemReader.Close();
            }
        }
        catch (Exception e1)
        {
            throw new Exception(e1.Message);
        }
        finally
        {
            conn.Close();
        }
    }

Solution

  • There's some unusual logic going on with conn.Open(). I see it used several times, but I think you mean to use conn2.Open() in the inner using statements after the first call.