Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:26+00:00 2026-05-29T10:31:26+00:00

EDIT After staring at this for 2 days, I do see one issue. I

  • 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();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:31:27+00:00Added an answer on May 29, 2026 at 10:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT 1 I apologize but after reading the 2 suggested articles I still don't
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
EDIT: After feedback from my original post, I've change the text to clarify my
EDIT: After realizing I have to make things public so to speak this is
What is Lazy Loading? [Edit after reading a few answers] Why do people use
Is there a way to convert from System.Windows.Forms.HtmlElement to mshtml.IHTMLElemenet3? --edit after answer accepted
I'm trying to immediately set a row to Edit mode after it is created.
I've got a treeview control, and have caught its after-label-edit event. I want to
edit: many thanks for all the answers. Here are the results after applying the
I have an application that uses MSWord automation to edit some documents, after they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.