c# - 确保此代码文件中定义的类与 'inherits' 属性匹配

标签 c# asp.net sql-server inheritance

我正在设置一个自定义选项卡,用户可以在其中查看和编辑数据。我需要建立一个受信任的 SQL 连接并将数据显示在 GridView 中。

我应该构建控制台应用程序还是网络应用程序?

我在下面提供了我的 .aspx 和 aspx.cs 文件。

运行时出现如下错误信息:

"Error: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or 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>

这是我的 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();
            }
        }
    }
}

最佳答案

虽然已经有一个选定的答案,而且我的答案并不完全适用于这个特定的问题,但我不止一次遇到过这个问题,而且偶尔会有其他原因。就在今天早上,我在尝试构建时遇到了这个错误,并且我正确地继承了 System.Web.UI.Page。事实上,我搜索了代码文件,一切看起来都很好。但是,经过仔细检查,我发现缺少的是条件语句上的一个右括号,它以某种方式导致了不相关的错误。

我并不是说我的答案是这个问题的正确答案,因为问题代码中左括号和右括号的数量似乎没问题,我只是说值得检查。如果这可以为​​一个人节省 10 分钟,那我的时间就值得了。谢谢!

关于c# - 确保此代码文件中定义的类与 'inherits' 属性匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037856/

相关文章:

c# - 带有 IDisposable 的简单 ConsoleProgram - 没有内存减少 - 我们有泄漏?

c# - 如何从 C# 中的 resx 中删除资源条目?

c# - 在 asp.net 中发送批量电子邮件的最佳方式

c# - 系统.Data.OleDb.OleDbException : Syntax error in INSERT INTO statement

c# - 从 C# 中的存储过程返回行 ID?

sql - 从字符串中提取日期并将其插入Microsoft SQL Server 2012字段

java - 删除对象时使用 Hibernate 违反 MS-SQL 中的 UNIQUE KEY 约束

c# - 集合被修改枚举操作可能不会执行

c# - root 的 XML 反序列化错误

asp.net - 安装后 Windows 字体不能立即在应用程序中使用?