0

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();
        }
    }
James
  • 3,765
  • 4
  • 48
  • 79
  • 1
    didn't read it all, but shouldn't you `open` your `conn` soon after the first `using`? also you're opening conn more than once! – vulkanino Feb 03 '12 at 14:31
  • @vulkanino It's opened on the 7th line. That's how I always have it set up. It works fine any other time, except that now it says it doesn't get closed with these inner queries. – James Feb 03 '12 at 14:33

1 Answers1

1

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.

Hadmacker
  • 36
  • 4
  • The last sounds like what I may have to do. I return 4 fields from the original query. I'll look into the datatable. Haven't done that before. – James Feb 03 '12 at 14:42
  • What vulkanino said. My original analysis was incorrectly suggesting a dataadapter (which is still a decent alternative to eliminate the multiple connections/multiple open datareaders) but I posted my corrected duplicate answer before I realized what vulkanino was saying. – Hadmacker Feb 03 '12 at 14:46
  • Yeah, I realized it right before Vulkanino edited his comment. I already changed that with no real change. – James Feb 03 '12 at 14:48
  • You're still opening the first strSQL right after you open conn2 the first time. It also needs to be changed to strSQL2. :) – Hadmacker Feb 03 '12 at 14:53
  • @Hadmacker Absolutely right. I've been staring at this for too long. Now I'm just getting an actual SQL Syntax error. I think we're on the right track. Let me fix this, and I'll get back with you. Thanks for the catch. – James Feb 03 '12 at 14:56