Article
ASP.NET 2.0: A Getting Started Guide
Installing SQL Server 2005 Express Edition
After making sure IIS, the .NET Framework, and the SDK are installed correctly, it's time to move forward and install the next piece of software that you'll be using as we work through this book: SQL Server 2005 Express Edition.
SQL Server 2005 is Microsoft's database solution for medium to large companies and enterprises. SQL Server 2005 can be quite expensive, it generally requires its own dedicated database server machine, and, at times, it necessitates that a certified database administrator (DBA) be employed to ensure its maintenance; yet it does offer a robust and scalable solution for larger web applications.
For the examples in this book, we'll use SQL Server 2005 Express Edition, which is free and sufficiently powerful for our needs. Unlike the expensive versions, SQL Server 2005 Express Edition doesn't ship with visual management utilities, but you can use another free tool from Microsoft--SQL Server Management Studio Express, which we'll install next)--for these purposes.
You can install SQL Server 2005 Express Edition as follows:
- Navigate to http://msdn.microsoft.com/vstudio/express/sql/, and click the Download Now link.
- In the next page, you can choose between SQL Server 2005 Express Edition, and SQL Server 2005 Express Edition with Advanced Services. The former will be fine for our purposes. Your system should meet the necessary requirements, so go ahead and click Download.
- Once the download has completed, double-click the downloaded executable file, and follow the steps to install the product. It's safe to use the default options all the way through, though it is a rather long process.
Provided that everything goes well, SQL Server 2005 Express Edition will be up and running at the end of the process. Like IIS, SQL Server runs as a service in the background, accepting connections to databases instead of web pages. The SQL Server is accessible at the address (local)\SqlExpress.
Installing SQL Server Management Studio Express
In order to use your SQL Server 2005 install effectively, you'll need some sort of administration tool that will allow you to work with your databases. SQL Server Management Studio Express is a free tool provided by Microsoft to allow you to manage your installation of SQL Server 2005.
To install SQL Server Management Studio Express, follow these steps:
- Navigate again to http://msdn.microsoft.com/vstudio/express/sql/, and click the Download Now link.
- This time, download the SQL Server Management Studio Express edition that corresponds to the SQL Server 2005 version that you installed previously.
- After the download completes, execute the file and follow the steps to install the product.
Once it's installed, SQL Server Manager Express can be accessed from Start > All Programs > Microsoft SQL Server 2005 > SQL Server Management Studio Express. When executed, it will first ask for your credentials, as Figure 1.12 illustrates.

Figure 1.12. Connecting to SQL Server
By default, when installed, SQL Server 2005 Express Edition will only accept connections that use Windows Authentication, which means that you'll use your Windows user account to log in to the SQL Server. Because you're the user that installed SQL Server 2005, you'll already have full privileges to the SQL Server. Click Connect to connect to your SQL Server 2005 instance.

Figure 1.13. Managing your database server
After you're authenticated, you'll be shown the interface in Figure 1.13, which gives you many ways to interact with, and manage, your SQL Server 2005 instance.
SQL Server Management Studio lets you browse through the objects inside your SQL Server, and even modify their settings. For example, you can change the security settings of your server by right-clicking the COMPUTER\SQLEXPRESS (where COMPUTER is the name of your computer), choosing Properties, and selecting Security from the panel, as shown in Figure 1.14.

Figure 1.14. Changing server settings with SQL Server Management Studio
SQL Server and Instances
You can run multiple SQL Servers on the one computer simultaneously?each SQL Server is called an instance of SQL Server. How is this useful? Imagine you have a production server that runs two applications with two separate databases on the same instance of SQL Server. If, for some reason, we need to restart SQL Server for the first application, the second application's database will become unavailable while the restart is taking place. If the second application's database was operating on a second instance of SQL Server, we wouldn't have such a problem--the second application would continue working without missing a beat.
Each instance of SQL Server requires a name. The default instance name for SQL Server 2005 Express Edition is SQLEXPRESS. When connecting to your database server, you must specify both the name of the computer and the name of the SQL Server instance in the form ComputerName/InstanceName. You can see this specification back in Figure 1.12 and Figure 1.13, where we're connecting to an instance called SQLEXPRESS on a computer called VM2.
Installing Visual Web Developer 2005
Visual Web Developer automates many of the tasks that you'd need to complete yourself in other environments, and includes many powerful features. For the first exercises in this book, we'll recommend you use a simple text editor such as Notepad, but you'll gradually learn how to use Visual Web Developer to ease some of the tasks we'll tackle.
So let's install this tool to make sure we'll have it ready when we need it.
- Go to http://msdn.microsoft.com/vstudio/express/vwd/ and click the Download link.
- Execute the downloaded file.
- Accept the default options. At one point, you'll be asked about installing Microsoft MSDN 2005 Express Edition, which is the product's documentation. It wouldn't hurt to install it, but you need to be patient, because it's quite big. (Note that you've already installed the .NET Framework 2.0 documentation, together with the SDK.)
Bonus!
If you've already installed the .NET Framework 2.0 SDK, you've already installed Microsoft MSDN 2005 Express Edition.
In this book, we'll start using Visual Web Developer to build real web applications in Chapter 5, Building Web Applications. Until then, we'll create examples using Notepad (or another simple text editor) so you're prepared to take full advantage of the features offered by Visual Web Developer when the time comes to use it.
Writing your First ASP.NET Page
For your first ASP.NET exercise, we'll create the simple example shown in Figure 1.15.

Figure 1.15. Your first ASP.NET page
Let's get started! Open your text editor (Notepad is fine). If you have software that creates ASP.NET pages automatically, such as Visual Studio .NET or Visual Web Developer 2005 Express Edition, please don't use it yet?while these are great tools that allow you to get up and running quickly, they do assume that you already understand how ASP.NET works.
So, open your text editor, and create a new file named FirstPage.aspx in the Learning folder you created earlier. Start editing FirstPage.aspx by entering the HTML for our page, shown below:
Example 1.2. FirstPage.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>My First ASP.NET 2.0 Page</title>
</head>
<body>
<p>Hello there!</p>
<p>The time is now: </p>
</body>
</html>
So far, so good, right? Now, let's add some ASP.NET code that will create the dynamic elements of the page, starting with the time.
Example 1.3. FirstPage.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>My First ASP.NET 2.0 Page</title>
</head>
<body>
<p>Hello there!</p>
<p>The time is now:
<asp:Label runat="server" id="timeLabel" /></p>
</body>
</html>
We've added an <asp:Label/> tag to the document. This is a special tag that lets us insert dynamic content into the page. The asp: part of the tag name identifies it as a built-in ASP.NET tag. ASP.NET comes with numerous built-in tags; <asp:Label/> is one of the simplest.
The runat="server" attribute identifies the tag as something that needs to be handled on the server. In other words, the web browser will never see the <asp:Label/> tag; when the page is requested by the client, ASP.NET sees it and converts it to regular HTML tags before the page is sent to the browser. It's up to us to write the code that will tell ASP.NET to replace this particular tag with the current time.
To do this, we must add some script to our page. ASP.NET gives you the choice of a number of different languages to use in your scripts. The two most common languages are VB and C#. Let's take a look at examples using both. Here's a version of the page in VB:
Example 1.4. FirstPage.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>My First ASP.NET Page</title>
<script runat="server" language="VB">
Sub Page_Load(sender As Object, e As EventArgs)
timeLabel.Text = DateTime.Now.ToString()
End Sub
</script>
</head>
<body>
<p>Hello there!</p>
<p>The time is now:
<asp:Label runat="server" id="timeLabel" /></p>
</body>
</html>
Here's the same page written in C#:
Example 1.5. FirstPage.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>My First ASP.NET Page</title>
<script runat="server" language="C#">
protected void Page_Load(object sender, EventArgs e)
{
timeLabel.Text = DateTime.Now.ToString();
}
</script>
</head>
<body>
<p>Hello there!</p>
<p>The time is now:
<asp:Label runat="server" id="timeLabel" /></p>
</body>
</html>
Clues for Case Sensitivity
Note that C#, unlike VB, is case-sensitive. If you type the case of a letter incorrectly, the page won't load. If these languages look complicated, don't worry: you'll learn more about them in Chapter 3, VB and C# Programming Basics.
Both versions of the page achieve exactly the same thing. You can even save them both, giving each a different filename, and test them separately. If you've never done any server-side programming before, the code may look a little scary. But before we analyze it in detail, let's load the page and test that it works. Using your web browser, load http://localhost/Learning/FirstPage.aspx. Whether you load the C# version or the VB version, the output should look like Figure 1.15.
No Time?
If the time isn't displayed in the page, chances are that you opened the file directly in your browser instead of loading it through your web server. Because ASP.NET is a server-side language, your web server needs to process the file before it's sent to your browser for display. If it doesn't get access to the file, the ASP.NET code is never converted into HTML that your browser can understand, so make sure you load the page by entering an actual HTTP URL (such as http://localhost/Learning/FirstPage.aspx), not a local path and filename (such as C:\WebDocs\Learning\FirstPage.aspx).
What happens there? Let's break down the new elements of this page.
Example 1.6. FirstPage.aspx (excerpt)
<script runat="server">
This tag marks the start of server-side code, or the code declaration block. Like the <asp:Label/> tag, this <script> tag uses the runat="server" attribute to let ASP.NET know that the tag should be processed before sending the page to the browser.
Example 1.7. FirstPage.aspx (excerpt)
Sub Page_Load(sender As Object, e As EventArgs)
Example 1.8. FirstPage.aspx (excerpt)
protected void Page_Load(object sender, EventArgs e)
{
I won't go into too much detail here. For now, all you need to know is that you can write script fragments that are run in response to different events, such as a button being clicked or an item being selected from a drop-down list. What the first line of code basically says is, "execute the following script whenever the page is loaded." Note that C# groups code into blocks with curly braces ({ and }), while Visual Basic uses statements such as End Sub to mark the end of a particular sequence. So, the curly brace in the C# code above ({) marks the start of the script that will be executed when the page loads for the first time.
Finally, here's the line that actually displays the time on the page:
Example 1.9. FirstPage.aspx (excerpt)
timeLabel.Text = DateTime.Now.ToString()
Example 1.10. FirstPage.aspx (excerpt)
timeLabel.Text = DateTime.Now.ToString();
As you can see, these .NET languages have much in common, because they're both built on the .NET Framework. In fact, the only difference between the ways the two languages handle the above line is that C# ends lines of code with a semicolon (;). In plain English, here's what this line says:
Set the Text of timeLabel to the current date and time, expressed as text.
Note that timeLabel is the value we gave for the id attribute of the <asp:Label/> tag where we want to show the time. So, timeLabel.Text, or the Tex property of timeLabel, refers to the text that will be displayed by the tag. DateTime is a class that's built into the .NET Framework; it lets you perform all sorts of useful functions with dates and times. The .NET Framework has thousands of these classes, which do countless handy things. The classes are collectively known as the .NET Framework Class Library.
The DateTime class has a property called Now, which returns the current date and time. This Now property has a method called ToString, which expresses that date and time as text (a segment of text is called a string in programming circles). Classes, properties, and methods: these are all important words in the vocabulary of any programmer, and we'll discuss them in more detail a little later in the book. For now, all you need to take away from this discussion is that DateTime.Now.ToString() will give you the current date and time as a text string, which you can then tell your <asp:Label/> tag to display. The rest of the script block simply ties up loose ends:
Example 1.11. FirstPage.aspx (excerpt)
End Sub
</script>
Example 1.12. FirstPage.aspx (excerpt)
}
</script>
The End Sub in the VB code, and the } in the C# code, mark the end of the script that's to be run when the page is loaded, and the </script> tag marks the end of the script block.
One final thing that's worth investigating is the code that ASP.NET generated for you. It's clear by now that your web browser receives only HTML (no server-side code!), so what kind of HTML was generated for that label? The answer is easy to find! With the page displayed in your browser, you can use the browser's View Source feature to view the page's HTML code. Here's what you'll see:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>My First ASP.NET Page</title>
</head>
<body>
<p>Hello there!</p>
<p>The time is now:
<span id="timeLabel">6/20/2006 8:00:49 PM</span></p>
</body>
</html>
Notice that all the ASP.NET code has gone! Even the script block has been completely removed, and the <asp:Label/> tag has been replaced by a <span> tag (which has the same id attribute as the <asp:Label/> tag we used) that contains the date and time.
That's how ASP.NET works. From the web browser's point of view, there's nothing special about an ASP.NET page; it's just plain HTML like any other. All the ASP.NET code is run by your web server and converted to plain HTML that's sent to the browser. So far, so good: the example above was fairly simple. The next chapter will get a bit more challenging as we investigate some valuable programming concepts.
Getting Help
As you develop ASP.NET web applications, you will undoubtedly have questions that need answers, and problems that need to be solved. The ASP.NET support web site was developed by Microsoft as a portal for the ASP.NET community to answer the questions and solve the problems that developers encounter while using ASP.NET. The support web site provides useful information, such as news, downloads, articles, and discussion forums. You can also ask questions of the experienced community members in the SitePoint Forums.
Summary
In this chapter, you learned about .NET. You also learned of the benefits of ASP.NET and that it's a part of the .NET Framework.
First, you learned about the components of ASP.NET and how to locate and install the .NET Framework. Then, we explored the software that's required not only to use this book, but also in order for you or your company to progress with ASP.NET development.
You've gained a solid foundation in the world of ASP.NET! The next chapter will build on this knowledge as we begin to introduce you to ASP.NET in more detail, covering page structure, the languages that you can use, various programming concepts, and the finer points of form processing.