Project Management

Dive into .NET with Web Matrix

ASP.NET Web Matrix is a free download that lets you create ASP.NET Web applications, services, etc. Here's an overview of Web Matrix, as well as the steps you need to take to create a .NET Web solution using Web Matrix.

Microsoft is tipping the scales in its favor with ASP.NET Web Matrix, a free download that lets you create ASP.NET Web applications, services, etc. This IDE runs on Windows 2000 and Windows XP machines. Here's an overview of Web Matrix, as well as the steps you need to take to create a .NET Web solution using Web Matrix.

Web Matrix looks similar to Visual Studio .NET, and some of the functionality is the same as VS.NET. One thing that you'll miss right away (if you use Microsoft development tools) is IntelliSense; however, there is a class browser that lets you peruse the different classes available from the .NET Framework. Using the class browser, you can devise a plan of attack for creating the code necessary to complete the functionality you're trying to implement.

Other highlights include the SELECT, INSERT, UPDATE, DELETE, and Send Email code wizards that generate the code you need in order to run queries against a SQL or Microsoft Access database and send e-mail. This is a great training tool for anyone who wants to take the fabricated code and personalize it to create new functionality. Another highlight is the Custom Controls tab in the Toolbox. By right-mouse clicking, you can add controls from the online components library to your IDE. This is useful if you want to bypass all the headaches involved with creating your own custom components—well, it's a headache if you don't have any desire to develop these components.

Without going into too much detail, let's create a Web service that selects data from a SQL table and returns the results as a dataset. Fortunately, there's an online tutorial that will walk you through the process of creating such a solution. I'll follow the online tutorial somewhat, though I'll address some of the items that are left out. Also, I'll mix-and-match a couple of tutorials so you get a grasp of how to create a more realistic solution.

Follow the steps in the tutorial for creating the Web service. But instead of using the Filename, Class, Language, and Namespace that the tutorial uses, I suggest you use "mydata.asmx", "MyDataClass", "C#", "MyData", respectively. You'll end up with code in the code window that looks like this:

<%@ WebService language="C#" class="MyDataClass" %>

using System;
using System.Web.Services;
using System.Xml.Serialization;

public class MyDataClass {

    public int Add(int a, int b) {
        return a + b;

I'll change this a bit by adding debugging to my solution—changing the first line to include Debug="true". I'll also declare a namespace for my class by adding the following line right before the class constructor line:


If you don't add this, Web Matrix will set the namespace to "". This is fine for development, but you'll need to change it for release.

The wizard adds the method "Add" to your class; you can delete this code. We'll add a method that selects the data and returns a dataset. You can do this by clicking the Code Wizard tab on the Toolbox. Then, click and drag the SELECT button to a blank space in your class block. When you release the mouse button, the wizard appears.

The first screen of the wizard asks you to choose a database connection. Set the Select A Database field to <New Database Connection>. Set the Select A Database Type to SQL Server/MSDE Database. At this point, in order to use this, you'll have to have an available SQL Server or the MSDE installed. I happen to have the MSDE installed, so I'll use that connection. If you don't have either of these, you might want to consider installing MSDE 2000.

Next, click the Create button. Enter the server name in the Server field (or keep the default (local) if you're using a local instance of SQL or MSDE). Keep the Windows Authentication selection. (This will be important down the line.) Select the Database that you wish to connect to and click OK. (I'm connecting to a local database called MAIN_DB.) Follow the next few wizard windows to create the SELECT query. When you get to the last screen, where you'll see the Finish button, change the method name to GetMyData and make sure the return value is set to DataSet. Click Finish.

Now you'll see the generated code in the code window. Here's the code:

    public System.Data.DataSet GetMyData() {
        string connectionString = "server=\'(local)\'; trusted_connection=true;
        System.Data.IDbConnection dbConnection = new
        string queryString = "SELECT [zips].* FROM [zips]";
        System.Data.IDbCommand dbCommand = new
        dbCommand.CommandText = queryString;
        dbCommand.Connection = dbConnection;
        System.Data.IDbDataAdapter dataAdapter = new
        dataAdapter.SelectCommand = dbCommand;
        System.Data.DataSet dataSet = new System.Data.DataSet();
        return dataSet;

Notice that I added the [WebMethod] declaration to the resulting code. This identifies the method as a Web method. I also added the public scope qualifier to the method.

Note: Once you download and install Web Matrix, you can look through the class browser, which includes links to all of the online documentation for the .NET classes and associated methods/properties.

Running the Web service

First, save your file to a project directory of your choice. This directory will become a virtual root directory, so make sure you're comfortable with the location. Either go to View | Start and hit [F5], or click the Play button on the toolbar. You'll receive a prompt to start the application in either the included Web Matrix Server or IIS. The Web Matrix Server makes things easy to test out and comes with Web Matrix. However, I decided to test the code on my IIS installation. To do so, choose Use Or Create An IIS Virtual Root. Then, type the name of the alias you wish to give the new virtual root. (I chose MatrixTest.) Note: You should save your file in the Application Directory field. You may have to adjust the security settings on this directory later.

If all goes well, you'll see a page with a link to your method name. If not, you'll probably have one of two problems: 1) IIS doesn't have an association for .asmx files; or 2) IIS cannot access the folder where the file is located. To cure the first problem, you can either add the extension mappings yourself, or you can reinstall the current .NET Framework. To map the extensions yourself, you'll need the Internet Information Systems Manager, which is usually located under Administrative Tools in the Control Panel. Using the IIS Manager, you'll navigate to your virtual directory where your ASP.NET Web service resides (which, in my case, is the MatrixTest directory). To do so, right-click on the directory and choose Properties. Click on the Configuration. . . button under the Virtual Directory tab. Under the Mappings tab in the next window, click the Add button to add a new mapping. If you don't already have the .asmx extension mapped, map it to %windir%\Microsoft.NET\Framework\<version>\aspnet_isapi.dll, where <version> is the version of your .NET Framework installation.

If you get an access violation error when trying to run your Web service, you may have to allow access to the directory where your ASP.NET files are located. (This is the virtual root location from earlier.) Using Windows Explorer, navigate to the virtual root folder. Right-click on the folder and choose Properties. Under the Security tab in the next window, check to make sure the ASP.NET Machine Account has permissions to access this directory. If not, click the Add button and enter <computer name>\ASPNET as the new account username where <computer name> is the name of the computer where IIS is located. The minimum access requirements are List Folder Contents and Read.

Now you should be able to access your Web service. Try running the Web service from Web Matrix again. If you get the method name link, click on the method name. You'll get a screen with information on using your Web service but, most importantly, you're provided with an Invoke button to test your Web service. Click Invoke to invoke your Web service. A separate window should open displaying the XML data from your query.

If, like me, you get an HTTP 404 Page not found error, the best thing to do is to uninstall your .NET Framework installation and reinstall the latest .NET Framework.

The class browser that comes with Web Matrix allows you to research the .NET model. On the left-hand side of the browser, you'll find the available interfaces and classes that you need. Double-clicking on these interfaces or classes displays the methods and properties available on the right-hand side. It will also provide you with a link to online documentation for the associated class, method, or property.

You can follow the tutorials on Microsoft's ASP.NET home page to create a Web service client to test your Web service. You'll also find ASP.NET tutorials, forums, and newsgroups, as well as downloads for the .NET Framework v1.1 and MSDE 2000.

Keep your developer skills sharp by automatically signing up for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.

Editor's Picks

Free Newsletters, In your Inbox