So I have an ASP.NET web site (not web application) I'm making in VS2010 with C#. It runs fine on my machine, but when I upload it to the site it's hosted on, it won't compile, giving: "CS0246: The type or namespace name 'DataAccess' could not be found (are you missing a using directive or an assembly reference?)"
I've been using the copy web site feature in VS and had no problems until I wanted to put my own class in the App_Code folder and use it. I read in other answers about changing the .cs properties to "Compile" instead of "Content", but there's no such option for me in the properties of the file... only File Name, Full Path, and Custom Tool. Here's the code from the .cs file:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
/// <summary>
/// Provides access to SQL Server database.
/// </summary>
///
public class DataAccess
{
//Variables & public properties ***********************************
private string connectionString = "";
private int recordCount = -1;
/// <summary>
/// Property: gets count of records retrieved or changed
/// </summary>
public int Count
{
get
{
return recordCount;
}
}
//Class constructor is executed when object is initialized ***********
/// <summary>
/// Connection string name in web.config file is required to initialize DataAccess
/// </summary>
/// <param name="ConnectionName">Name of web.config connection string</param>
public DataAccess(string ConnectionName)
{
if (WebConfigurationManager.ConnectionStrings[ConnectionName] == null) {
throw new Exception("Cannot find connection string named '" +
ConnectionName + "' in web.config");
}
//Get connection string from web.config.
connectionString = WebConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString;
}
/// <summary>
/// Executes SELECT statement and returns results in dataTable
/// </summary>
/// <param name="SQL">Select SQL statement</param>
/// <returns></returns>
public DataTable FillDataTable(string SQL)
{
SqlConnection _objConn = new SqlConnection(connectionString);
SqlDataAdapter objAdapter = new SqlDataAdapter(SQL, _objConn);
DataTable dt = new DataTable();
try {
objAdapter.Fill(dt);
}
catch (SqlException ex) {
throw new Exception("Error in SQL:" + SQL, ex);
}
catch (Exception ex) {
throw ex; //Bubbling exception up to parent class
}
finally {
_objConn.Close();
}
recordCount = dt.Rows.Count;
return dt;
}
/// <summary>
/// Executes "non-query" SQL statements (insert, update, delete)
/// </summary>
/// <param name="SQL">insert, update or delete</param>
/// <returns>Number of records affected</returns>
public int ExecuteNonQuery(string SQL)
{
SqlConnection _objConn = new SqlConnection(connectionString);
try {
_objConn.Open();
SqlCommand objCmd = new SqlCommand(SQL, _objConn);
recordCount = objCmd.ExecuteNonQuery();
}
catch (SqlException ex) {
throw new Exception("Error in SQL:" + SQL, ex);
}
catch (Exception ex) {
throw new Exception(ex.Message); //Rethrowing exception up to parent class
}
finally { _objConn.Close(); }
return recordCount;
}
public int ExecuteScalar(String SQL)
{
SqlConnection _objConn = new SqlConnection(connectionString);
int intID;
try {
_objConn.Open();
SqlCommand objCmd = new SqlCommand(SQL, _objConn);
intID = Convert.ToInt32(objCmd.ExecuteScalar());
}
catch (SqlException ex) {
throw new Exception("Error in SQL:" + SQL, ex);
}
catch (Exception ex) {
throw new Exception(ex.Message); //Rethrowing exception up to parent class
}
finally { _objConn.Close(); }
return intID;
}
}//end class
And from my page:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Initialize dataAccess class
DataAccess myDA = new DataAccess("A05Customers");
//Populate dataTable and bind to GridView Control
string strSQL = "Select * from tblCustomers";
gvCustomers.DataSource = myDA.FillDataTable(strSQL);
gvCustomers.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="font-family: Verdana">
<h2>GridView</h2>
<hr style="color: #0000FF" width="800" />
<br />
<asp:GridView ID="gvCustomers" runat="server" BackColor="#FFFFCC" BorderColor="#336699" BorderStyle="Inset" BorderWidth="5px" CellPadding="2" CellSpacing="4" />
<br />
</div>
</form>
</body>
</html>
Thanks for your help!
By default websites don't compile .cs files. You will need to do a few different things.
Set each class file to "compile" in the properties for that class file. You can see this option by clicking the class file, viewing the properties in the properties explorer window, and changing the Build Action dropdown list to "Compile."
If the above doesn't help, remove the class from the app_code and drop it in the root folder.