title: Right code to retrieve data from sql server database
body: <p>I have some problems in database connection and wonder if I have something wrong in my code. Please review. This question is related: <a href="https://stackoverflow.com/questions/4547070/switch-between-databases-use-two-databases-simultaneously">Switch between databases, use two databases simultaneously</a> question.</p>
  
  <pre><code>cs="Data Source=mywebsite.com;Initial Catalog=database;User Id=root;Password=toor;Connect Timeout=10;Pooling='true';"
  
  using (SqlConnection cnn = new SqlConnection(WebConfigurationManager.ConnectionStrings["cs"].ConnectionString))
  {
      using (SqlCommand cmmnd = new SqlCommand("", cnn))
      {
          try
          {
              cnn.Open();
  
              #region Header &amp; Description
              cmmnd.Parameters.Add("@CatID", SqlDbType.Int).Value = catId;
              cmmnd.CommandText = "SELECT UpperID, Title, Description FROM Categories WHERE CatID=@CatID;";
  
              string mainCat = String.Empty, rootCat = String.Empty;
  
              using (SqlDataReader rdr = cmmnd.ExecuteReader())
              {
                  if (rdr.Read())
                  {
                      mainCat = rdr["Title"].ToString();
                      upperId = Convert.ToInt32(rdr["UpperID"]);
                      description = rdr["Title"];
                  }
                  else { Response.Redirect("/", false); }
              }
  
              if (upperId &gt; 0) //If upper category exists add its name
              {
                  cmmnd.Parameters["@CatID"].Value = upperId;
                  cmmnd.CommandText = "SELECT Title FROM Categories WHERE CatID=@CatID;";
                  using (SqlDataReader rdr = cmmnd.ExecuteReader())
                  {
                      if (rdr.Read())
                      {
                          rootCat = "&lt;a href='x.aspx'&gt;" + rdr["Title"] + "&lt;/a&gt; &amp;raquo; ";
                      }
                  }
              }
              #endregion
  
              #region Sub-Categories
              if (upperId == 0) //show only at root categories
              {
                  cmmnd.Parameters["@CatID"].Value = catId;
                  cmmnd.CommandText = "SELECT Count(CatID) FROM Categories WHERE UpperID=@CatID;";
  
                  if (Convert.ToInt32(cmmnd.ExecuteScalar()) &gt; 0)
                  {
                      cmmnd.CommandText = "SELECT CatID, Title FROM Categories WHERE UpperID=@CatID ORDER BY Title;";
  
                      using (SqlDataReader rdr = cmmnd.ExecuteReader())
                      {
                          while (rdr.Read())
                          {
                              subcat.InnerHtml += "&lt;a href='x.aspx'&gt;" + rdr["Title"].ToString().ToLower() + "&lt;/a&gt;\n";
                              description += rdr["Title"] + ", ";
                          }
                      }
                  }
              }
              #endregion
          }
          catch (Exception ex) { HasanG.LogException(ex, Request.RawUrl, HttpContext.Current); Response.Redirect("/", false); }
          finally { cnn.Close(); }
      }
  }
  </code></pre>
  
  <p>The random errors I'm receiving are:</p>
  
  <ul>
  <li>A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)</li>
  <li>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</li>
  <li>Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.</li>
  <li>Cannot open database "db" requested by the login. The login failed. Login failed for user 'root'.</li>
  </ul>