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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:34:54+00:00 2026-05-13T15:34:54+00:00

I have a page where a user can insert some order about some product.

  • 0

I have a page where a user can insert some order about some product. In that page there are also live statistics which is being refreshed every 20 seconds to reflect the current price of a product using asp.net timer. This live panel fetching these prices from a database.

When sometimes an order is being inserted, I get an exception saying that the connection is closed and the insert command cannot continue. I suspect that when the insert command is given, if at that exact moment the live update is being refreshed, then they both need to access the database, and they are using the same SqlConnection Object. So when one finishes, the connection object being closed even if the other one is using the same connection object.

Before closing a connection, how can I be sure that no other methods are using that connection?

The code of my connection class is given below, for better clarification –

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.IO.Ports;


public class ConnectionHelper
{

#region Private Variables
static string BOconnectionString = "";
static SqlConnection BOcon = null;
static string STRSconnectionString = "";
static SqlConnection STRScon = null;
static SqlTransaction tSTRSTrans = null;

public static SqlTransaction STRSTrans
{
    get { return ConnectionHelper.tSTRSTrans; }
    set { ConnectionHelper.tSTRSTrans = value; }
}
#endregion


#region Constructor
ConnectionHelper() { }
#endregion



#region Public Functions 

public static SqlConnection getSTRSConnection()
{
    STRSconnectionString = ConfigurationManager.ConnectionStrings["STRS_WEB_DB2ConnectionString"].ConnectionString;
    try
    {
        if ((STRScon == null) || (STRScon.State == ConnectionState.Closed))
        {
            STRScon = new SqlConnection(STRSconnectionString);
            STRScon.Open();
            //tSTRSTrans = STRScon.BeginTransaction(IsolationLevel.RepeatableRead);
        }


    }
    catch (Exception ex)
    {
        throw new Exception("Error occurred when Connection is going to be opened" + ex.Message);
    }
    return STRScon;
}
public static void closeSTRSConnection()
{
    if ((STRScon != null) || (STRScon.State == ConnectionState.Open))
    {
        STRScon.Close();
    }        
}

#endregion

}

If a method want to access the database, they are obtaining and closing the connection in the following way –

con = ConnectionHelper.getSTRSConnection();
........
........
ConnectionHelper.closeSTRSConnection();
  • 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-13T15:34:54+00:00Added an answer on May 13, 2026 at 3:34 pm

    I don’t think you should use this approach for a web application, I see your intent is to utilize a common connection, but consider refactoring your code so that instead of attempting to use an existing connection i.e. “getSTRSConnection”, you simple open a connection | do your work | and close it. As a general practice, opening connections as late as possible is a good idea, and then closing the connection as soon as the work is done is another good idea. Remember that these connections are an expensive resource and should only live during the time they are needed to persist information to your database.

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

Sidebar

Related Questions

I have a JSP page, so the user can insert the time when he/she
I have a page where a user can upload a file along with some
I have a page which contains a dynamically generated grid, where a user can
I have a form on a page that a user can use to create
I have a list of items on my web page that the user can
I have a jsp page that is trying to reference some user defined classes.
I have a page where a user can import data to the site. either
I have a page where the user can dynamically add file upload boxes. Adding
I have a page where the user can edit various content using buttons and
I have this PHP page where the user can select and un-select items. The

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.