Vì vậy, tôi có một trang web ASP.NET (không phải ứng dụng web) tôi đang tạo trong VS2010 với C#. Nó chạy tốt trên máy tính của tôi, nhưng khi tôi tải nó lên trang web được lưu trữ, nó sẽ không biên dịch, đưa ra: "CS0246: Không thể tìm thấy loại tên hoặc không gian tên 'DataAccess' (bạn đang thiếu chỉ thị sử dụng hoặc một tham chiếu lắp ráp?) "Trang web ASP.NET không thể xem tệp .cs trong thư mục App_Code
Tôi đã sử dụng tính năng sao chép trang web trong VS và không có vấn đề gì cho đến khi tôi muốn đặt lớp của riêng mình trong thư mục App_Code và sử dụng nó. Tôi đọc trong các câu trả lời khác về việc thay đổi thuộc tính .cs thành "Biên dịch" thay vì "Nội dung", nhưng không có tùy chọn nào cho tôi trong thuộc tính của tệp ... chỉ Tên tệp, Đường dẫn đầy đủ và Công cụ tùy chỉnh. Dưới đây là đoạn code từ file .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
Và từ trang của tôi:
<%@ 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>
Nhờ sự giúp đỡ của bạn!
Xem câu hỏi được hỏi trước đây. Đây là một câu hỏi thường gặp. http://stackoverflow.com/questions/89570/app-code-folder-issues – David