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 7709331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:50:06+00:00 2026-06-01T00:50:06+00:00

In my testing project, I have a static class called FixtureSetup which I use

  • 0

In my testing project, I have a static class called FixtureSetup which I use to setup my integration testing data for validation.

I use the same SqlCommand and SqlParameter variable (not the object itself) within that class, repeatedly, using the same variable references over and over, assigning new SqlCommand and SqlParameter objects each time. My connection itself is created once and passed into the methods performing the setup, so each setup uses it’s own distinct connection reference, and while the same conn is used multiple times, it’s always in a linear sequence.

In one such method, I ran into a very odd situation, where my SqlCommand variable simply appears to have gotten tired.

        cmd = new SqlCommand("INSERT INTO Subscription (User_ID, Name, Active) VALUES (@User_ID, @Name, @Active)", conn);
        parameter = new SqlParameter("@User_ID", TestUserID); cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Name", "TestSubscription"); cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Active", true); cmd.Parameters.Add(parameter);
        cmd.ExecuteNonQuery();

        cmd = new SqlCommand("SELECT Subscription_ID FROM [Subscription] WHERE Name = 'TestSubscription'", conn);
        parameter = new SqlParameter("@User_ID", TestUserID);
        cmd.Parameters.Add(parameter);
        using (dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                TestSubscriptionID = dr.GetInt32(dr.GetOrdinal("Subscription_ID"));
            }
        }

        cmd = new SqlCommand("INSERT INTO SubscriptionCompany (Subscription_ID, Company_ID) VALUES (@Subscription_ID, @Company_ID)", conn);
        parameter = new SqlParameter("@Subscription_ID", TestSubscriptionID); cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Company_ID", KnownCompanyId); cmd.Parameters.Add(parameter);
        cmd.ExecuteNonQuery();

In the above, at the last line shown, doing the same thing I’ve done quite literally in dozens of other places (insert data, read the ID column and capture it), I get the following:

SetUp : System.InvalidOperationException : ExecuteNonQuery requires an
open and available Connection. The connection’s current state is
closed. at
System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)

BUT – replace cmd with new variable myCmd, and everything works swimmingly!

        SqlCommand myCmd;
        myCmd = new SqlCommand("INSERT INTO Subscription (User_ID, Name, Active) VALUES (@User_ID, @Name, @Active)", conn);
        parameter = new SqlParameter("@User_ID", TestUserID); myCmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Name", "TestSubscription"); myCmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Active", true); myCmd.Parameters.Add(parameter);
        myCmd.ExecuteNonQuery();

        myCmd = new SqlCommand("SELECT Subscription_ID FROM [Subscription] WHERE Name = 'TestSubscription'", conn);
        parameter = new SqlParameter("@User_ID", TestUserID);
        myCmd.Parameters.Add(parameter);
        using (dr = myCmd.ExecuteReader())
        {
            while (dr.Read())
            {
                TestSubscriptionID = dr.GetInt32(dr.GetOrdinal("Subscription_ID"));
            }
        }

        myCmd = new SqlCommand("INSERT INTO SubscriptionCompany (Subscription_ID, Company_ID) VALUES (@Subscription_ID, @Company_ID)", conn);
        parameter = new SqlParameter("@Subscription_ID", TestSubscriptionID); myCmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@Company_ID", KnownCompanyId); myCmd.Parameters.Add(parameter);
        myCmd.ExecuteNonQuery();

What the heck is going on here? Did my command var just get tired???

What clued me to the “fix” was I noticed in my tracing that in my “read the id” block, my cmd.Parameters block had only ONE parameter in it, the 2nd one added, and when I forced the first cmd.Parameters.Add line to execute again, the number of parameters in the list dropped to 0. That’s what prompted me to try a method level SqlCommand…cause I had the crazy idea that my cmd was tired… Imagine my shock when I apparently turned out to be right!

Edit: I’m not recycling any objects here – just the variable reference itself (static SqlCommand at the class level). My apologies for the earlier confusion in my wording of the question.

  • 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-06-01T00:50:08+00:00Added an answer on June 1, 2026 at 12:50 am

    Big Edit:

    From: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.close.aspx

    You must explicitly call the Close method when you are through using
    the SqlDataReader to use the associated SqlConnection for any other
    purpose.

    The Close method fills in the values for output parameters, return
    values and RecordsAffected, increasing the time that it takes to close
    a SqlDataReader that was used to process a large or complex query.
    When the return values and the number of records affected by a query
    are not significant, the time that it takes to close the SqlDataReader
    can be reduced by calling the Cancel method of the associated
    SqlCommand object before calling the Close method.

    so try:

    using (dr = cmd.ExecuteReader())
    {
        while (dr.Read())
        {
            TestSubscriptionID = dr.GetInt32(dr.GetOrdinal("Subscription_ID"));
        }
        dr.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Testing project that contains a csv file which a webtest uses
I have domain classes as: package mnm.schedule class Project { static hasMany = [
I have a project that I would like to start beta testing soon, it
I have a project structure like: src/CMakeLists.txt src/test/component1/CMakeLists.txt src/test/component2/CMakeLists.txt For the testing, I'm using
We have a python project that we want to start testing using buildbot. Its
Short version: I have a Django project under development & testing (not yet into
I'm working on an integration testing project in .NET. The testing framework executable starts
I have a project which is heavily JavaScript based (e.g. node.js, backbone.js, etc.). I'm
I use Hibernate to generate my database automatically for testing, and I have some
In my Cocoa Touch static library project, I have a target set apart for

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.