error with codefile inheritenace

D@rren

New member
Joined
Nov 22, 2013
Messages
4
Programming Experience
3-5
Hi,
I suspect this has been covered already, but I have been unable to locate an answer that resolves the issue I have so far. Been searching around for a while now and im sure its a simple issue that im just miss-understanding.

I am new to ASP.NET forms but am familiar with VBA in excel and various other programming languages.
I have a local SQL database created for testing with, and im using Visual Studio Express 2013.

The long term goal is to create a web front end with IIS integrated security that shows data from and allows input into the SQL backend to create new entries.

I managed to get a asp.net web form created that connected to the database and then displayed the SQL query results in a repeater table with no issues. The problem I have is that I then moved into the master.page setup of the project and now have trouble with my codefile being referenced.

All files live in one directory:

Default.aspx
VB.NET:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" CodeFile="Site.master.vb"  Inherits="Site.master" %>

Site.master.vb
VB.NET:
Imports System.Data.SqlClient

Namespace Site
    Partial Class master
        Inherits System.Web.UI.Page

Site.master
VB.NET:
<%@ Master Language="VB" CodeFile="Site.master.vb" Inherits="Site.master" %>

All I want to do is have one code file that has subroutines such as my SQL connect script, and search queries etc. That can be used on various aspx pages. Unfortunately with the current setup I receive the following error:

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: ASPNET: 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).

Source Error:

Line 2:
Line 3: Namespace Site
Line 4: Partial Class master
Line 5: Inherits System.Web.UI.Page
Line 6:

Source File: C:\VisualStudio\WebSite\Salesbook\Site.master.vb Line: 4
 
It looks to me that you have mixed up the content and master classes and directives there.
You can get preconfigured master and content pages that is defined and linked correctly by using the 'Add New item' menu. For master select "Master Page" template, and for content select "Web Form" but enable checkbox "select master page" also to bring up a dialog where you select the master to use for that content page. I use 2010 version, but this should not be much different in 2013 version.

This may also be useful if you want to call code in master from content page: How to: Reference ASP.NET Master Page Content
 
It looks to me that you have mixed up the content and master classes and directives there.
You can get preconfigured master and content pages that is defined and linked correctly by using the 'Add New item' menu. For master select "Master Page" template, and for content select "Web Form" but enable checkbox "select master page" also to bring up a dialog where you select the master to use for that content page. I use 2010 version, but this should not be much different in 2013 version.

This may also be useful if you want to call code in master from content page: How to: Reference ASP.NET Master Page Content

Sorry for the late reply, I was of work last week.

I tried that previously by creating a new page with a master template file ticked and reviewed the content which was as follows:

VB.NET:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" %>

With this code at the top of default.aspx, I still get the class error.
If I edit the entry at the top of the master file to remove the codefile and inherits statements, I then cannot use the functions and controls in my page. So its definitely something to do with the way I have the inherits in place?

I did not want to end up with multiple code files all over the place. Should the codefile and inherit statements be in the master file or the content pages below, or both?
 
Think I may have just sussed it after a bit of trial and error.

Ive changed my site.master page to show as follows:
VB.NET:
<%@ Master Language="VB" debug="true" %>

default.aspx shows as follows:
VB.NET:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" CodeFile="Site.master.vb" Inherits="Site.master" %>

This clears the original error, and does seem to be running the code from my separate code file, although ive got another error with the code now, but I believe that to be unrelated. Ill keep playing and see what I find out.

Thanks for the assistance so far.
 
While it may be possible to share code-behind files between different pages, you shouldn't, it will only be a source for confusion. The master page is a separate page in this context, and it also inherits a different class than a regular page.

To use the methods defined in the master class in the content pages read the article "How to: Reference ASP.NET Master Page Content" that I linked to, it is done with MasterType directive.

Also, if these are methods that is not related to functionality of the master page itself, then you are better off not putting them there, instead use a class file in App_Code folder.
 
If I have code to connect to a SQL database that may be used on several pages, you would recommend to keep the code to a codefile per page and duplicate this?

The sub I was building connects to a database and then outputs the data to a repeater to put it into a table onto the page. I started coming across problems referencing objects and variables when using multiple pages and files. No doubt due to incorrect coding on my side :)
Im just trying to find the best way to build the site and how to reference code in different files to bring it all together at present. I thought adding it all into one codefile would be simpler, but that sounds like part of my problem perhaps?
 
One code file is fine, just don't share a code-behind file between different pages. One page = one code-behind file.

As for database stuff, explore the possibilities of the IDE to generate database interaction for you.
 
Back
Top