title: Calling class from code behind
body: <p>I am still new to ASP.net and I'm learning how to call classes.  I've looked around for tutorials, but not all are specific to ASP.net 4.0 so I'm not sure if I should apply them.</p>
  
  <p>Right now, I am trying to connect to my SQL database.  My webconfig file has been set up with the connectionstring "dbConnectionString" and is working properly when I've tested it with GridViews.  What I'd like to do now is access the database from code behind.</p>
  
  <p>I've seen some ways to accomplish this, but I'd like the most efficient, resuable way.  I've tried to adopt the answer listed here: <a href="https://stackoverflow.com/questions/9806166/how-to-create-sql-connection-with-c-sharp-code-behind-access-the-sql-server-the">How to create sql connection with c# code behind, access the sql server then conditionally redirect?</a> however, I'm getting an error.</p>
  
  <p>I am doing this with C# as a website, not a web application.  Here is my code behind for Login.aspx.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  using SqlComm; // Is this how I connect to my class from this page????
  
  
  public partial class Login : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          if (Page.IsValid)
          {
              //no code written yet
          }
      }
  
  }
  </code></pre>
  
  <p>My class in the App_Code folder, file name SQLComm.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data;
  using System.Data.SqlClient;
  using System.Web;
  
  /// &lt;summary&gt;
  /// SQL Query class
  /// &lt;/summary&gt;
  public class SqlComm
  {
      // Connection string
      static string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
  
      // Execute sql command with no value to return
      public static void SqlExecute(string sql)
      {
          using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
          {
              SqlCommand cmd = new SqlCommand(sql, conn);
              cmd.Connection.Open();
              cmd.ExecuteNonQuery();
          }
      }
  
      // Execute SQL query and return a value
      public static object SqlReturn(string sql)
      {
          using (SqlConnection conn = new SqlConnection(PjSql.dbcs()))
          {
              conn.Open();
              SqlCommand cmd = new SqlCommand(sql, conn);
              object result = (object)cmd.ExecuteScalar();
              return result;
          }
      }
  
      // Retrieve an entire table or part of it
      public static DataTable SqlDataTable(string sql)
      {
          using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
          {
              SqlCommand cmd = new SqlCommand(sql, conn);
              cmd.Connection.Open();
              DataTable TempTable = new DataTable();
              TempTable.Load(cmd.ExecuteReader());
              return TempTable;
          }
      }
  
      // Execute a stored procedure with 1 parameter
      // Returning a value or just executing with no returns
      public static object SqlStoredProcedure1Param(string StoredProcedure, string PrmName1, object Param1)
      {
          using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
          {
              SqlCommand cmd = new SqlCommand(StoredProcedure, conn);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.Add(new SqlParameter(PrmName1, Param1.ToString()));
              cmd.Connection.Open();
              object obj = new object();
              obj = cmd.ExecuteScalar();
              return obj;
          }
      }
  }
  </code></pre>
  
  <p>As you can see, I haven't written any code to actually use the class yet, but the "using SQLComm;" line itself is giving me this error:</p>
  
  <pre><code>Compiler Error Message: CS0246: The type or namespace name 'SqlComm' could not be found (are you missing a using directive or an assembly reference?)
  </code></pre>
  
  <p>As I'm still new, I'm unsure where to go from here.  I seem to have included everything contained in the answer on the page I linked above.  What else and I missing?</p>
  
  <p><strong>Edit: I read this post regarding a similar issue: <a href="https://stackoverflow.com/questions/4672262/asp-net-2-0-web-site-cant-access-classes-in-app-code">asp.NET 2.0 Web Site Can&#39;t Access Classes in App_Code</a> 
  Could this be because I am doing a website and not a web application, so the App_Code folder is being handled differently when I FTP the files over?</strong></p>