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

  • Home
  • SEARCH
  • 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 8570385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:32:46+00:00 2026-06-11T18:32:46+00:00

The problem is that I have used my SqlConnection as a public static connection,

  • 0

The problem is that I have used my SqlConnection as a public static connection, thinking this might be the problem causing form time to time an error :

*the connection is not open or the connection was already open

So is it ok to use one statement of SqlConnection in a static class?

So that I could Declare it only once, I know I could use connectionString in web.config

ConfigurationManager.ConnectionStrings["conn"].ConnectionString ...

but I like it to stay unrelated to web.config settings or servers name.

  • ReEdit :

as it is realy two methods within same class theres also another class in that main class
but this is not what’s important rather than using same connection for all
executions ! so you say that even though i re edited with right code of my helprer class
this is wrong ?

public static class myDBhelper
{
 public static SqlConnection Conn = new SqlConnection ("server=(local);Initial Catalog=dbName;Integrated Security=True");

        public static int ExecSQLint(string TblintSQL)
        {
            int anIntValue=0;
            SqlCommand TblintCMD = new SqlCommand(TblintSQL, Conn);
            try
            {
                Conn.Open();
                anIntValue = Convert.ToInt32(TblintCMD.ExecuteScalar());
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception("No Can Do: " + ex.Message);
            }
            finally
            {
                Conn.Close();
            }
          return anIntValue;
        }



        public static string ExecSQLstring(string TblStrSQL)
        {
          string strValue="";
          SqlCommand TblStrCMD = new SqlCommand(TblStrSQL, Conn);
            try
            {
                Conn.Open();
                strValue = TblStrCMD.ExecuteScalar().ToString();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception("No Can Do: " + ex.Message);
            }
            finally
            {
                Conn.Close();
            }

            return strValue;
        }


}

The main issue I suspect is those two options :

SqlConnection Conn = new SqlConnection("Data Source=(local);Integrated Security=True;database=dbName")

in my DBhelper class I was using this declaration

SqlConnection Conn = new SqlConnection("server=(local);Initial Catalog=dbName;Integrated Security=True");

could that be unstable or error prone ?

p.s.: I am executing commands via try catch

            try
            {
                Conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception("No Can Do: " + ex.Message);
            }
            finally
            {
                Conn.Close();
            }

Is Using statement more appropriate? Although it is not my problem I was thinking… if I am already trying to do it ‘by the book’…

Is any method here actually wrong among those ?

  • 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-11T18:32:47+00:00Added an answer on June 11, 2026 at 6:32 pm

    Keeping Connection as static is not a common way to use connection to database. It could lead to exception as you mentioned when application is working on web or multi-thread environment.

    Image that thread 1 executing command 1 is the same connection with thread 2 executing command 2. Your ex: TblintCMD and TblStrCMD. When thread 1 finishs, it closed connection, meanwhile thread 2 is still executing command on close connection

    Your two options are not the problem.

    The best way is to use using keyword and create connection when needed:

    using (var connection = new SqlConnection("yourConnectionString"))
    {
        connection.Open();
        ...
    }
    

    using is similar with:

    var connection =  new SqlConnection("connectionString");
    try
    {
        connection.Open();
        ....
    }
    finally
    {
        connection.Close();
    }
    

    So, you don’t need to know when to close Connection.

    Behind the scene, ADO.NET uses connection pool to manage connections for you automatically, so you should not care much how many connections open.

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

Sidebar

Related Questions

I tweaked this script that I used from JqueryUi and I have a problem.
I have a problem regarding the ksoap2. My problem is that the project used
The problem: we have one application that has a portion which is used by
i have bought a template that have built in contact form problem is that
The problem that I have is somehow very specific. I have to implement a
I have a simple problem that I have not been able to find an
We have the problem that we have the open project files in our SVN
I have a problem that I have not faced before: It seems that the
I have been Googling a problem that I have with trying to integrate the
I frequently run into the problem that I have to preserve state between several

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.