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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:02:42+00:00 2026-05-25T01:02:42+00:00

We have a helper class that we use to call stored procs on SQL

  • 0

We have a helper class that we use to call stored procs on SQL Server. The helper function looks like this:

using (sqlCon = new SqlConnection(connectionString))
        {

            // Create database command object
            sqlCmd = sqlCon.CreateCommand();
            sqlCmd.CommandTimeout = commandTimeout;

            // Set command text AND command type
            sqlCmd.CommandText = procedureName;
            sqlCmd.CommandType = CommandType.StoredProcedure;

            // Set command parameters
            if (paramCollection != null)
            {
                foreach (DatabaseParameter dbParam in paramCollection)
                {
                    SqlParameter sqlParam = sqlCmd.Parameters.Add(dbParam.ParameterName, dbParam.ParameterType);
                    sqlParam.Direction = dbParam.ParameterDirection;
                    sqlParam.Value = dbParam.ParameterValue;
                    if (dbParam.ParameterSize != -1)
                        sqlParam.Size = dbParam.ParameterSize;
                    if (dbParam.ParamPrecision != -1)
                        sqlParam.Precision = (byte)dbParam.ParamPrecision;
                    if (dbParam.ParamScale != -1)
                        sqlParam.Scale = (byte)dbParam.ParamScale;
                }
            }

            try
            {
                sqlCon.Open();
            }
            catch
            {
                SqlConnection.ClearAllPools();
                sqlCon.Open();
            }

            // Prepare command
            sqlCmd.Prepare();

            // Execute the statement
            sqlCmd.ExecuteNonQuery();

            if (sqlCmd.Parameters.Contains("@Result"))
                return sqlCmd.Parameters["@Result"].Value;
            else
                return "Completed";
        }

So we are making sure connections are closed properly.
The app is a multi threaded service and all the threads calls this method quite regularly. We do lock (thisobject) {} sections around the code that calls the above helper method to prevent the threads from stealing each others connections.

The connection.Open is inside a try catch with the ClearAllpools to clear broken connections.

However, we intermittently through the day get the following list of errors at random intervals.

The connection’s current state is connecting.

or

The connection’s current state is open.

The errors happen about once in every several thousand times the code is called, so it’s quite hard to troubleshoot. Has anybody seen anything similar or ideas on what could be wrong?

  • 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-25T01:02:43+00:00Added an answer on May 25, 2026 at 1:02 am

    I’ve run across these types of errors when dealing with a multi-threaded app, even when using a lock. After endless hours of debugging, I never found the problem and ended up trying to re-connect several times before giving up. To date, I haven’t seen the error arise again.

    int retries = 5;
    while( true )
    {
        try
        {
            DbCommand cmd = GetCommand( sql );
            using( DbConnection conn = cmd.Connection )
            {
                conn.Open();
                // do stuff
                break;
            }
        }
        catch
        {
            Thread.Sleep( 1000 );
            if( retries-- <= 0 )
            {
                throw;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ helper class that I use with objective-C. I would like
I have a helper class that is just a bunch of static methods and
I have a helper class pulling a string from an XML file. That string
I have a base class for many tests that has some helper methods they
I currently have a class file with the following enumeration: using System; namespace Helper
the situation is like this: im creating a Logger class that can write to
I have written an ASP.NET HttpModule and I have a static helper class that
can i call helper functions inside controller classes? let's say i have this controller
Using C#, visual studio 2008, SQL Server 2000 (and 2008) I use a DataLayer
I have a data mapping helper class that uses generics and reflection. Since I'm

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.