Tech

Guides
 

Web Matrix, anyone?

By Phillip Perkins, Builder.com
Wednesday, November 17, 2004 03:22 PM
Seeking a free entry into the .NET platform? Try ASP.NET Web Matrix with this starter tutorial.

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.

Creating a Web Service
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 {

    [WebMethod]
    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:

 [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 <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:

    [WebMethod]
    public System.Data.DataSet GetMyData() {
        string connectionString = "server=\'(local)\'; trusted_connection=true;
 database=\'MAIN_DB\'";
        System.Data.IDbConnection dbConnection = new
 System.Data.SqlClient.SqlConnection(connectionString);
        
        string queryString = "SELECT [zips].* FROM [zips]";
        System.Data.IDbCommand dbCommand = new
 System.Data.SqlClient.SqlCommand();
        dbCommand.CommandText = queryString;
        dbCommand.Connection = dbConnection;
        
        System.Data.IDbDataAdapter dataAdapter = new
 System.Data.SqlClient.SqlDataAdapter();
        dataAdapter.SelectCommand = dbCommand;
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(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 that 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.


See also:  Web sites

WORTHWHILE?

0

0 votes
Blog

Talkback 0 comments

There are currently no comments for this post.


Guest user

Guest user

Level: 
Joined: —
Already a member? Log in »



 

Loading...

Whitepapers/Case Studies

Downloads

Web Technologies News



Tech Jobs Now!

Tags

  1. business applications
  2. c#
  3. developer
  4. html
  5. industry
  6. java
  7. justin james
  8. microsoft .net
  9. microsoft corp.
  10. microsoft visual studio
  11. programming
  12. protocols and platforms
  13. server
  14. soa
  15. software engineering / development
  16. tool
  17. web
  18. web browser
  19. web services
  20. web sites