Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 3. | 5
Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 3
Understanding Namespaces
Because ASP.NET is part of the .NET Framework, we have access to all the goodies that are built into it in the form of the .NET Framework Class Library. This library represents a huge resource of tools and features in the form of classes, all organized in a hierarchy of namespaces. When we want to use certain features that .NET provides, we have only to find the namespace that contains that functionality, and import that namespace into our ASP.NET page. Once we’ve done that, we can make use of the .NET classes in that namespace to achieve our own ends.
For instance, if we wanted to access a database from a page, we would import the namespace that contains classes for this purpose, which happens to be the System.Data.OleDb namespace. The dots (.) here indicate different levels of the hierarchy I mentioned—in other words, the System.Data.OleDb namespace is grouped within the System.Data namespace, which in turn is contained in the System namespace.
To import a particular namespace into an ASP.NET page, we use the Import directive. Consider the following excerpt from an ASP.NET page; it imports the System.Data.OleDb namespace, which contains classes called OleDbConnection, OleDbCommand, and OleDbDataReader. Importing the namespace lets us use these classes in a subroutine to display records from an Access database:
<%@ Import Namespace="System.Data.OleDb" %> <html> <head> <script runat="server" language="VB"> Sub ReadDatabase(s As Object, e As EventArgs) Dim objConn As New OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Database\books.mdb") Dim objCmd As New OleDbCommand("SELECT * FROM BookList", _ objConn) Dim drBooks As OleDbDataReader objConn.Open() drBooks = objCmd.ExecuteReader() While drBooks.Read() Response.Write("<li>") Response.Write(drBooks("Title")) End While objConn.Close() End Sub </script> </head>
<%@ Import Namespace="System.Data.OleDb" %> <html> <head> <script runat="server" language="C#"> void ReadDatabase(Object s, EventArgs e) { OleDbConnection objConn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\Database\\books.mdb"); OleDbCommand objCmd = new OleDbCommand("SELECT * FROM BookList", objConn); OleDbDataReader drBooks; objConn.Open(); drBooks = objCmd.ExecuteReader(); while (drBooks.Read()) { Response.Write("<li>"); Response.Write(drBooks["Title"]); } objConn.Close(); } </script> </head>
Don’t worry too much about the code right now (we cover this in detail in Chapter 6, Database Design and Development). Suffice it to say that, as we’ve imported that namespace, we have access to all the classes that it contains, and we can use them to get information from an Access database for display on our page.
Specifically, the classes from System.Data.OleDb that are used in the above code are:
- OleDbConnection
Used for connecting to the database
- OleDbCommand
Used for creating a statement of contents to read from the database.
- OleDbConnection
Used for connecting to the database
- OleDbCommand
Used for creating a statement of contents to read from the database
- OleDbDataReader
Used for actually reading contents from database
VB.NET and C# are great programming languages because they offer a structured way of programming. By structured, I mean that code is separated into modules, where each module defines classes that can be imported and used in other modules. Both languages are relatively simple to get started with, yet offer features sophisticated enough for complex, large-scale enterprise applications.
The languages’ ability to support more complex applications—their scalability—stems from the fact that both are object oriented programming (OOP) languages. But ask a seasoned developer what OOP really is, and they’ll start throwing out buzzwords and catch phrases that are sure to confuse you—terms like polymorphism, inheritance, and encapsulation. In this section, I aim to explain the fundamentals of OOP and how good OOP style can help you develop better, more versatile Web applications down the road. This section will provide a basic OOP foundation angled towards the Web developer. In particular, we’ll cover the following concepts:
Objects
Properties
Methods
Classes
Scope
Events
Inheritance
In OOP, one thinks of programming problems in terms of objects, properties, and methods. The best way to get a handle on these terms is to consider a real world object and show how it might be represented in an OOP program. Many books use the example of a car to introduce OOP. I’ll try to avoid that analogy and use something friendlier: my dog, an Australian Shepherd named Rayne.
Rayne is your average great, big, friendly, loving, playful mutt. You might describe him in terms of his physical properties: he’s gray, white, brown, and black, stands roughly one and a half feet high, and is about three feet long. You might also describe some methods to make him do things: he sits when he hears the command "Sit", lies down when he hears the command "Lie down", and comes when his name is called.
So, if we were to represent Rayne in an OOP program, we’d probably start by creating a class called Dog. A class describes how certain types of objects look from a programming point of view. When we define a class, we must define the following two things:
- Properties
Properties hold specific information relevant to that class of object. You can think of properties as characteristics of the objects that they represent. Our Dog class might have properties such as Color, Height, and Length.
- Methods
Methods are actions that objects of the class can be told to perform. Methods are subroutines (if they don’t return a value) or functions (if they do) that are specific to a given class. So the Dog class could have methods such as sit(), and lie_down().
Once we’ve defined a class, we can write code that creates objects of that class, using the class a little like a template. This means that objects of a particular class expose (or make available) the methods and properties defined by that class. So, we might create an instance of our Dog class called Rayne, set its properties accordingly, and use the methods defined by the class to interact with Rayne, as shown in Figure 3.3.
This is just a simple example to help you visualize what OOP is all about. In the next few sections, we’ll cover properties and methods in greater detail, talk about classes and class instances, scope, events, and even inheritance.
As we’ve seen, properties are characteristics shared by all objects of a particular class. In the case of our example, the following properties might be used to describe any given dog:
Color
Height
Length
In the same way, the more useful ASP.NET Button class exposes properties including:
Width
Height
ID
Text
ForeColor
BackColor
Unfortunately for me, if I get sick of Rayne’s color, I can’t change it. ASP.NET objects, on the other hand, let us change their properties very easily in the same way that we set variables. For instance, we’ve already used properties when setting text for the Label control, which is actually an object of class Label in the namespace System.Web.UI.WebControls:
lblMyText.Text = "Hello World"
lblMyText.Text = "Hello World";
In this example, we’re using a Label control called lblMyText. Remember, ASP.NET is all about controls, and, as it’s built on OOP, all control types are represented as classes. In fact, as you’ll learn in Chapter 4, Web Forms and Web Controls, all interaction with ASP.NET pages is handled via controls. When we place a control on a page, we give it a name through its id attribute, and this ID then serves as the name of the control. Rayne is an object. His name, or ID, is Rayne. Rayne has a height of eighteen inches. The same holds true for the Label control. The Label control’s name or ID in the previous example is lblMyText. Next, we use the dot operator (.) to access the property Text that the object exposes and set it to the string "Hello World."
Created: March 27, 2003
Revised: July 5, 2004
URL: http://webreference.com/programming/asp_net3/1


Find a programming school near you