Yaknowss
Yaknowss

Reputation: 43

Make sure that the class defined in this code file matches the 'inherits' attribute

I am setting up a custom tab where users can view and edit data. I need to establish a trusted SQL connection and have the data displayed in a grid view.

Should I build a console or web app?

I have provided my .aspx and aspx.cs files below.

I get the error message below while running it:

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

Here is my Default.aspx code:

<%@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>

Here is my Default.aspx.cs code:

///<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();
            }
        }
    }
}

Upvotes: 2

Views: 23150

Answers (4)

Emi
Emi

Reputation: 1

I had this problem as I was using visual studio 2005, I just removed the namespace section of the code and that cleared all the errors

I made it like so:

public partial class bla : System.Web.UI.Page { all your codes and stuff }

And that was how it worked for me.

Upvotes: 0

Venkata Krishna
Venkata Krishna

Reputation: 15112

If you want to display data in a gridview, then console application doesn't make sense.. its gotta be a web application. Coming to your error.. post the top line in your aspx file here it looks something like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs"
    Inherits="Login" MasterPageFile="~/MasterPage.master" %>

Upvotes: 1

Francisco Garcia
Francisco Garcia

Reputation: 1044

Although there is already a selected answer, and my answer doesn't exactly apply to this particular problem, I've encountered this more than once and there are occasionally alternate causes. Just this morning I had this error when I attempted a build and I was correctly inheriting from System.Web.UI.Page. In fact I scoured the code files and everything looked totally fine. Upon close inspection, though, I discovered what was missing was a single closing bracket on a conditional statement that somehow caused the unrelated error.

I'm not saying my answer is the correct answer for this problem as the count of open and closing brackets seems fine in the question's code, I'm just saying it's worth checking. If this saves a person 10 minutes, it was worth my time. Thanks!

Upvotes: 10

Tieson T.
Tieson T.

Reputation: 21221

Your code-behind page must inherit from System.Web.UI.Page, like so:

public partial class _Default : System.Web.UI.Page
{

}

Upvotes: 7

Related Questions