title: Correct SqlConnection declaration
body: <p>The problem is that I have used my <code>SqlConnection</code> as a public static connection, thinking this might be the problem causing form time to time an error :</p>
  
  <blockquote>
    <p>*the connection is not open or the connection was already open</p>
  </blockquote>
  
  <p>So is it ok to use one statement of <code>SqlConnection</code> in a static class?</p>
  
  <p>So that I could <em>Declare</em> it only once, I know I could use <code>connectionString</code> in <code>web.config</code></p>
  
  <pre><code>ConfigurationManager.ConnectionStrings["conn"].ConnectionString ...
  </code></pre>
  
  <p>but I like it to stay unrelated to <code>web.config</code> settings or servers name.</p>
  
  <ul>
  <li>ReEdit :</li>
  </ul>
  
  <p>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 ?</p>
  
  <pre><code>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;
          }
  
  
  }
  </code></pre>
  
  <p>The main issue I suspect is those two options :</p>
  
  <pre><code>SqlConnection Conn = new SqlConnection("Data Source=(local);Integrated Security=True;database=dbName")
  </code></pre>
  
  <p>in my <code>DBhelper</code> class I was using this declaration </p>
  
  <pre><code>SqlConnection Conn = new SqlConnection("server=(local);Initial Catalog=dbName;Integrated Security=True");
  </code></pre>
  
  <p>could that be unstable or error prone ?</p>
  
  <p>p.s.: I am executing commands via try catch</p>
  
  <pre><code>            try
              {
                  Conn.Open();
                  cmd.ExecuteNonQuery();
              }
              catch (System.Data.SqlClient.SqlException ex)
              {
                  throw new Exception("No Can Do: " + ex.Message);
              }
              finally
              {
                  Conn.Close();
              }
  </code></pre>
  
  <p>Is <code>Using</code> statement more appropriate? Although it is not my problem I was thinking... if I am already trying to do it 'by the book'...</p>
  
  <p>Is any method here actually <strong>wrong</strong> among those ? </p>