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?)"
所以我有一个ASP.NET网站(不是Web应用程序)我在VS2010中用C#制作。它在我的机器上运行正常,但是当我将它上传到它托管的站点时,它将无法编译,给出:“CS0246:找不到类型或命名空间名称'DataAccess'(你是否缺少using指令或装配参考?)“
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:
我一直在使用VS中的复制网站功能,并且在我想将自己的类放在App_Code文件夹中并使用它之前没有任何问题。我在其他答案中读到了将.cs属性更改为“编译”而不是“内容”,但在文件属性中没有这样的选项...只有文件名,完整路径和自定义工具。这是.cs文件中的代码:
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!
谢谢你的帮助!
4 个解决方案
#1
20
By default websites don't compile .cs files. You will need to do a few different things.
默认情况下,网站不会编译.cs文件。你需要做一些不同的事情。
-
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.
如果上述方法无效,请从app_code中删除该类并将其放在根文件夹中。
#2
5
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.
你有app_code文件夹它是Web应用程序的根文件夹(不仅是文件夹或虚拟文件夹)。看来Asp.NET无法看到app_code文件夹。
Conent and compile options are available only in WebApplication project.
Conent和编译选项仅在WebApplication项目中可用。
#3
1
I asked this question before starting an account, otherwise I'd edit it.
我在开始帐户之前问过这个问题,否则我会编辑它。
I have the same error after:
之后我有同样的错误:
-
Checking that I have a Web Site, not Web Application, thus no Compile/Content properties for the .cs file.
检查我有一个网站,而不是Web应用程序,因此没有.cs文件的编译/内容属性。
-
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
检查App_Code文件夹的位置。我尝试将GridView.aspx放在root下以及它自己的文件夹中,同样的错误。 \ _ App_Code DataAccess.cs \ App_Data \ GridView GridView.aspx Web.config
-
Moving the class from App_Code to root. Same error.
将类从App_Code移动到root。同样的错误。
-
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.
这最后我认为会起作用,但没有。使用“发布网站”功能上载预编译的代码。我可以看到它现在是bin文件夹中的DataAccess.dll,并且根目录中有一个PrecompiledApp.config文件。同样的错误。
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.
更新:解决了它:服务器上的文件夹结构就像本地文件夹一样,但我在子文件夹中包含了所有内容... App_Code和App_Data必须位于服务器的根目录中。
#4
0
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.
您可能需要在部署之前编译应用程序。目前,ASP.NET无法了解未附加到.aspx / .ascx文件的.cs文件。我建议您查看Web部署项目或使用Visual Studio 2010“发布”选项。斯科特格思里给出了一个比我在这里更好的总结。
#1
20
By default websites don't compile .cs files. You will need to do a few different things.
默认情况下,网站不会编译.cs文件。你需要做一些不同的事情。
-
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.
如果上述方法无效,请从app_code中删除该类并将其放在根文件夹中。
#2
5
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.
你有app_code文件夹它是Web应用程序的根文件夹(不仅是文件夹或虚拟文件夹)。看来Asp.NET无法看到app_code文件夹。
Conent and compile options are available only in WebApplication project.
Conent和编译选项仅在WebApplication项目中可用。
#3
1
I asked this question before starting an account, otherwise I'd edit it.
我在开始帐户之前问过这个问题,否则我会编辑它。
I have the same error after:
之后我有同样的错误:
-
Checking that I have a Web Site, not Web Application, thus no Compile/Content properties for the .cs file.
检查我有一个网站,而不是Web应用程序,因此没有.cs文件的编译/内容属性。
-
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
检查App_Code文件夹的位置。我尝试将GridView.aspx放在root下以及它自己的文件夹中,同样的错误。 \ _ App_Code DataAccess.cs \ App_Data \ GridView GridView.aspx Web.config
-
Moving the class from App_Code to root. Same error.
将类从App_Code移动到root。同样的错误。
-
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.
这最后我认为会起作用,但没有。使用“发布网站”功能上载预编译的代码。我可以看到它现在是bin文件夹中的DataAccess.dll,并且根目录中有一个PrecompiledApp.config文件。同样的错误。
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.
更新:解决了它:服务器上的文件夹结构就像本地文件夹一样,但我在子文件夹中包含了所有内容... App_Code和App_Data必须位于服务器的根目录中。
#4
0
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.
您可能需要在部署之前编译应用程序。目前,ASP.NET无法了解未附加到.aspx / .ascx文件的.cs文件。我建议您查看Web部署项目或使用Visual Studio 2010“发布”选项。斯科特格思里给出了一个比我在这里更好的总结。