views:

67

answers:

4

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"&gt;

<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"&gt;
<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!

A: 

You probably need to compile your application before deploying it. At the moment ASP.NET has no way of knowing about .cs files that aren't attached to .aspx/.ascx files. I'd recommend looking at Web Deployment Projects or using the Visual Studio 2010 "Publish" option. Scott Guthrie gives a better summary than I ever could here.

nizmow
alternately he could try re-saving his web.config
drachenstern
That will re-start the application, but the .cs file in his "App_Code" folder will still never be compiled.
nizmow
A: 

Do you have app_code folder it the root folder of the web application (not only folder or virtual folder). It seems that Asp.NET can't see app_code folder.

Conent and compile options are available only in WebApplication project.

Matej
A: 

By default websites don't compile .cs files. You will need to do a few different things.

  1. 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."

  2. If the above doesn't help, remove the class from the app_code and drop it in the root folder.

Justin
A: 

I asked this question before starting an account, otherwise I'd edit it.

I have the same error after:

  1. Checking that I have a Web Site, not Web Application, thus no Compile/Content properties for the .cs file.

  2. Checking position of the App_Code Folder. I tried putting GridView.aspx under root as well as in its own folder, same error. \ \App_Code DataAccess.cs \App_Data \GridView GridView.aspx Web.config

  3. Moving the class from App_Code to root. Same error.

  4. This last I thought would work, but didn't. Used the Publish Web Site feature to upload precompiled code. I could see that it was now DataAccess.dll in the bin folder, and there was a PrecompiledApp.config file in the root. Same error.

UPDATE: solved it: the folder structure on the server was like the local one, but I had everything in a subfolder... App_Code and App_Data must be in the root of the server.

yeratowel