Yak*_*wss 2 c# sql-server asp.net inheritance
我正在设置一个自定义选项卡,用户可以在其中查看和编辑数据.我需要建立一个受信任的SQL连接,并在网格视图中显示数据.
我应该构建一个控制台或Web应用程序吗?
我在下面提供了我的.aspx和aspx.cs文件.
我在运行它时收到以下错误消息:
"错误:确保此代码文件中定义的类与'inherits'属性匹配,并且它扩展了正确的基类(例如Page或UserControl)".
这是我的Default.aspx代码:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的Default.aspx.cs代码:
///<summary>
///Demonstrates how to work with SqlConnection objects
///</summary>
class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnectionconn = newSqlConnection("Data Source=TestDB;Initial Catalog=Impresario;Integrated Security=SSPI");
SqlDataReaderrdr = null;
try
{
// 2. Open the connection
conn.Open();
// 3. Pass the connection to a command object
SqlCommandcmd = newSqlCommand("select * from LT_WEB_DONATIONS_EXTRA_INFO_TEMP", conn);
//
// 4. Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if(rdr != null)
{
rdr.Close();
}
// 5. Close the connection
if(conn != null)
{
conn.Close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*cia 10
虽然已经有一个选定的答案,而且我的答案并不完全适用于这个特定的问题,但我不止一次遇到这种情况,偶尔会有其他原因.就在今天早上,当我尝试构建并且我正确地从System.Web.UI.Page继承时,我遇到了这个错误.事实上,我搜索了代码文件,一切看起来都很好.然而,经过仔细检查,我发现缺少的是条件语句上的单个结束括号,它以某种方式导致了无关的错误.
我不是说我的答案是这个问题的正确答案,因为问题代码中的开括号和右括号的计数似乎很好,我只是说它值得检查.如果这可以节省一个人10分钟,那值得我花时间.谢谢!
您的代码隐藏页面必须继承System.Web.UI.Page,如下所示:
public partial class _Default : System.Web.UI.Page
{
}
Run Code Online (Sandbox Code Playgroud)