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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:03:14+00:00 2026-05-27T19:03:14+00:00

Im making a system which should be running 24/7, with timers to control it.

  • 0

Im making a system which should be running 24/7, with timers to control it. There are many calls to the database, and at some point, two methods are trying to open a connection, and one of them will fail. I’ve tried to make a retry method, so my methods would succeed. With the help from Michael S. Scherotter and Steven Sudit’s methods in Better way to write retry logic without goto, does my method look like this:

        int MaxRetries = 3;
        Product pro = new Product();
        SqlConnection myCon = DBcon.getInstance().conn();

        string barcod = barcode;

        string query = string.Format("SELECT * FROM Product WHERE Barcode  = @barcode");

        for (int tries = MaxRetries; tries >= 0; tries--) //<-- 'tries' at the end, are unreachable?. 
        {
            try
            {

                myCon.Open();
                SqlCommand com = new SqlCommand(query, myCon);
                com.Parameters.AddWithValue("@barcode", barcode);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    pro.Barcode = dr.GetString(0);
                    pro.Name = dr.GetString(1);
                }

                    break;
                }
                catch (Exception ex)
                {
                    if (tries == 0)
                        Console.WriteLine("Exception: "+ex);
                        throw;

                }
                }



        myCon.Close();
        return pro;

When running the code, the program stops at the “for(…..)”, and the exception: The connection was not closed. The connection’s current state is open… This problem was the reason why I’m trying to make this method! If anyone knows how to resovle this problem, please write. Thanks

  • 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-27T19:03:15+00:00Added an answer on May 27, 2026 at 7:03 pm

    Edited for new information

    How about using Transactions to preserve data integrity, getting on-the-fly connections for multiple access and wrapping them in Using statements to ensure connections are closed? eg

            Using (SqlConnection myCon = new SqlConnection('ConnectionString'))
            {
              myCon.Open();
              var transaction = myCon.BeginTransaction();    
              try 
              { 
                // ... do some DB stuff - build your command with SqlCommand but use your transaction and your connection
               var sqlCommand = new SqlCommand(CommandString, myCon, transaction);
               sqlCommand.Parameters.Add(new Parameter()); // Build up your params
               sqlCommand.ExecuteNonReader(); // Or whatever type of execution is best
               transaction.Commit();  // Yayy!
            } 
            catch (Exception ex) 
            { 
                transaction.RollBack();  // D'oh!
                // ... Some logging
            } 
    
            myCon.Close();
        }
    

    This way even if you forget to Close the connection, it will still be done implicitly when the connection gets to the end of its Using statement.

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

Sidebar

Related Questions

I'm currently making some system that will gather statistical reports from different sites, for
I have an application which is making use of the RSACryptoServiceProvider to decrypt some
I am making a BI system for a bank-like institution. This system should manage
I'm trying to update some older software which uses the now-declared-obsolete System.Web.Mail classes. Unfortunately,
Which Non-relational database should I choose(I know the question is a subjective one and
I want to build a windows Service, which should execute different methods at different
Hi all I'm making a system which is essentially a scheduled task system. I
I'm making a bash script which should create an ftp user. ftpasswd --passwd --file=/usr/local/etc/ftpd/passwd
I'm making a system called NCIV in PHP. In that system you can change
I'm making a rating system, and I have the following jQuery code on my

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.