When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, December 24, 2003

Accessing Common Code, Constants, and Functions in an ASP.NET Project

By Scott Mitchell


When moving from classic ASP development to ASP.NET, one thing many developers find themselves needing to do is port their existing reusable code base from classic ASP to ASP.NET. With classic ASP, many developers created include files, which are files that would contain common functions, code, and constants. These include files could then be used in any classic ASP page by simply adding an #include directive at the top of the page: <!--#include file="includeFile.asp"-->. Include files in classic ASP were useful for centralizing code so that common functions and constants didn't need to be repeated in numerous pages. (For more information on using #include in classic ASP, be sure to read: The Low-Down on #includes.)

- continued -

In ASP.NET, #include is still supported to a degree, but there are better ways to create a common repository of functions, code, and constants. In this article we'll first examine the optimal approach for storing Web application-wide constants; next, we'll look at how to provide a means for creating functions common to all ASP.NET Web pages in the Web application.

Storing Constants

There are many times when various Web pages in a Web application need access to a similar set of constant values. The most common scenario is when the Web pages access database data. These pages, if accessing the database directly, need to know the connection string to the database. One approach is to simply hard-code the database connection string in each data-accessing page, but this approach can lead to quite the headache if the connection string changes, since it requires manually editing all pages where the constant has been duplicated.

A better approach is to use a centralized repository for storing such constants. This approach allows for the constant's value to be spelled out in precisely one location. The Web pages that need to access the constant's value can do so through a bit of code. With this technique, needed changes to the constant require only a change in one place, and the pages that use the constant are automatically updated accordingly.

The question, then, is where should these constants be stored, and how can they be accessed? The ASP.NET Web.config file provides as good a place as any for storing simply scalar constants. The Web.config file can have an appSettings element added, in which you can provide a variable number of constant names and values. For example, a database connection string could be defined in the Web.config file like so:

  <!-- application specific settings -->
      <add key="connString" value="connection string" />


Then, from an ASP.NET Web page, a constant value can be retrieved with just one line of code, ConfigurationSettings.AppSettings("constantName"), like in:

Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connString"))

Note: you will need to import the System.Configuration namespace into your project for the above code examples to work...

In the Web.config file you can even add your own constants "group," akin to the appSettings. For more information on this technique, as well as a more in-depth discussion on using Web.config to store application-wide constants, be sure to read: Specifying Configuration Settings in Web.config.

Rather than using the Web.config technique, you can roll your own constants managing class, as Rachael Schoenbaum showed in her article Using XML to Share Constants Across Projects.

Creating Functions Common to All ASP.NET Pages in a Web Project

Many classic ASP developers have built up a rich set of functions that are used in a variety of their pages through #include files. Imagine that we had the following classic ASP function, defined in a #include file and used in a number of classic ASP pages:

Function DisplayEmployee(objRS)
  Dim str
  str = "<div>Name: " & objRS("Name") & " (" & objRS("SSN") & ")<br>" & _
        "Dept: " & objRS("Dept") & "</div>"
  DisplayEmployee = str
End Function

This VBScript function takes in a Recordset and displays the name, social security number, and department of the employee in a pre-formatted way. Such a function might be used in numerous pages when working with employees in order to display their vital information in a consistent format. Therefore, when migrating from classic ASP to ASP.NET, the classic ASP developer might want to create a central function repository in his ASP.NET Web application that would contain functions like DisplayEmployee().

To provide such functionality in an ASP.NET project, create a class in your ASP.NET Web application project and for each common function, create a Shared function in the class. (Shared functions, or "static functions" in C# terminology, are class methods that can be invoked without an instance of the class being first created. That is, if you have a class named ConstClass, and a method named DisplayEmployee(), you can invoke the method with: ConstClass.DisplayEmployee(), as opposed to have to create an instance of the ConstClass first...)

To create a new class using Visual Studio .NET, right click on the Project name in the solution explorer and choose to Add Class, as shown in the screenshot below. You will be asked to pick a name for the class, I would suggest using something descriptive like ConstClass.

Adding a new class via VS.NET

This will create a blank class file with code like:

Public Class ConstClass

End Class

Now, just add the function(s) you want to have accessible from all ASP.NET Web pages in this project. When adding the function (or sub), be sure to use the keyword Shared in the function's definition, as shown below. For example, we could port the VBScript DisplayEmployee() function, adding it to the class:

Public Class ConstClass
  Public Shared Function DisplayEmployee(ByVal reader as SqlDataReader)
    Dim str as String
    str = "<div>Name: " & reader("Name") & " (" & reader("SSN") & ")<br>" & _
        "Dept: " & reader("Dept") & "</div>"
    Return str
  End Function
End Class

Now, in an ASP.NET Web page this method can be called like so: ConstClass.DisplayEmployee(reader). The following example illustrates this common function in use:

Dim myConnection as New SqlConnection(connString)
Dim myCommand as New SqlCommand(SQL, myConnection)

Dim reader as SqlDataReader = myCommand.ExecuteReader()
While reader.Read()
End While



When moving from classic ASP to ASP.NET there is often a need to port over common functions and constants. If you are new to ASP.NET you might not be familiar with the options for providing common code to all ASP.NET Web pages in a given project. This article examined methods for sharing both constants and functions across ASP.NET Web pages in a Web application.

Happy Programming!

  • By Scott Mitchell

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article