X
Business

Diving into .NET with Web Matrix, part 1

Web Matrix is a design tool for ASP that's simpler to use than Visual Studio. Learn how to write ASP.NET pages with it.
Written by Phillip Perkins, Contributor

Web Matrix is a design tool for ASP that's simpler to use than Visual Studio. Learn how to write ASP.NET pages with it

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 email. 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 Listing A.

Listing A

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:

[WebService(Namespace="http://someplace.com/MyData")]

If you don't add this, Web Matrix will set the namespace to "http://tempuri.org/webservices". 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 . 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, in Listing B.

Listing B

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.

In next week's article, I'll run the Web service and discuss some of the hurdles I encountered when creating this service, namely, security and Web configuration settings. I'll also cover the class browser.

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.

Phillip Perkins' experience ranges from machine control and client server to corporate intranet applications.

Editorial standards