This documentation is archived and is not being maintained.

Walkthrough: Creating a Distributed Application 

In this walkthrough, you will create a multi-tiered, distributed, intranet application. The application will consist of three logical tiers: data, business object, and user interface. The data tier is a database in SQL Server. The business-object tier will handle accessing the data and distributing it to the clients. The user-interface tier will consist of both a Web-based application and a traditional Windows application.

The application you will build is a simple data application with lookup and edit. You will build a Windows client to display the Customers table from the SQL Server Northwind sample database. You will create an XML Web service to retrieve data that contains Customers information from the database. You will then build a Windows application that will communicate with this XML Web service to retrieve a dataset containing the author information.

You will create the XML Web service and the client applications on the same server, though the database may reside on another server. Both the application server and the database server will need to reside in the same intranet. This organization of projects allows you to use Windows authentication to gain access to the database and test your application. At the same, this organization removes many of the details of resource access so that you can see the interaction of the XML Web service and the client applications. This organization may not be exactly the architecture you want for applications you create on your own.

In order to complete this walkthrough, you will need:

  • Access to a server with the Northwind SQL Server sample database, configured for Integrated Windows authentication. The Northwind database is one of the sample databases that you can install with SQL Server.

  • A basic understanding of how data is handled in Visual Studio .NET. For more information, see Overview of ADO.NET.

One possible scenario for developing a distributed application is to create one tier at a time, perhaps beginning with the data layer, then moving to the middle-tier business-rule object, and finally creating the user interface. For this walkthrough, the data has already been generated and is available in the Northwind database in SQL Server. Therefore, the walkthrough will start with creating the business object — the XML Web service — followed by building the two user interfaces — a Web Forms page and a Windows Form. The process for this walkthrough is as follows:

  1. Create the middle-tier business object

    1. Create an ASP.NET Web Service project

    2. Create and configure a database connection and a dataset schema

    3. Expose the dataset from your business object

  2. Create the user interface

    1. Create a Windows user interface

  3. Deploy the solution or add more features

Create the Middle-Tier Business Object

The business object you crea te will run on a Web server to provide the performance and scalability necessary for a distributed application. In addition, you will implement the business object as an XML Web service so that clients can use standard Internet protocols to communicate with your business object from any platform. For details, see Programming the Web with XML Web Services.

In this walkthrough, the XML Web service component will hold the data connections and dataset definition. XML Web service methods will then be added to expose the dataset, which will make it possible for other applications to view and modify the dataset.

The XML Web service will expose two methods. The first, GetCustomers, will return a dataset from the database. The second, UpdateCustomers, will update the database with changes from the user.

To create an ASP.NET Web Service project

  1. On the File menu, point to New then Web Site to display the New Web Site dialog box.

  2. Select ASP.NET Web Service in the Visual Studio Installed Templates pane.

  3. In the Location box, enter the name of the Web server (on your development computer) with the name of the project, http://ServerName/CustomersWebService, and then click OK.


    Because the Web server is on your computer, you can use the server name LOCALHOST.

  4. The CustomersWebService project is added to the solution. Note that the web service is initialized in the default language you have chosen for Visual Studio.

  5. In Solution Explorer, click Service.asmx to select it.

  6. In the Properties window, set the File Name property of Service to CustomersService.

    In this component you will create a connection to the data store and obtain an instance of the data using a dataset.

Create and Configure a Database Connection and a DataSet Schema

You will add a dataset to your application and then use the TableAdapter Configuration Wizard to generate a typed TableAdapter specific to the Customers table of the database.

To create a database connection and data adapter

  1. From the Website Menu, choose Add New Item. The Add New Item dialog box opens.

  2. Select Dataset and click Add

    The dataset is added to your project, and the TableAdapter Configuration Wizard is started.

  3. In the TableAdapter Configuration Wizard, click New Connection to create your connection.

    The Add Connection dialog box opens, and Microsoft SQL Server is pre-selected as the database type.

  4. In the Add Connection dialog box, enter the name of the SQL Server where the Northwind database is installed. If you have SQL Server on the local computer, enter (local).

  5. Select Use Windows Authentication for the logon information.


    If you do not have integrated security set up on your system, see your network administrator.

  6. Select the Northwind database from the list.

  7. Click Test Connection to validate the information you provided, and then click OK to establish the connection.

    A new database connection is created and added to the dropdown box in the TableAdapter Configuration Wizard.


    If the database connection fails, see your database administrator.

  8. Ensure that your database is selected in the dropdown box and click Next.

  9. Click Next again to save the connection string to the configuration file.

  10. In the Choose a Command Type pane, ensure that Use SQL Statements is selected and click Next.

  11. In the Enter a SQL Statement pane, type SELECT * FROM Customers and click Next.

  12. In the Choose Methods to Generate pane click Finish.

    You have created a connection to the database, a typed dataset, and generated the table adapter that interacts with the database.

  13. From the File menu, choose Save All

You need to configure the security settings of your project to work with Integrated Security. You do this by turning off anonymous access and turning on impersonation.

To configure Integrated Windows authentication for the project, you need to both change the project files and configure the project using the Internet Information Services tool.

To configure Integrated Windows authentication

  1. Start the Internet Information Services tool. It can be run from the Administrative Tools of the Control Panel. (For more information on starting this tool, see your Windows help documentation.)

  2. Expand the node for your server.

  3. Expand the Default Web Site node.

  4. Right-click the node for CustomersWebService and choose Properties from the shortcut menu.

  5. Click the Directory Security tab.

  6. Click the Edit button in the Anonymous access and authentication control section.

  7. Clear the Anonymous Access check box.

  8. Select the Integrated Windows authentication check box. You have now configured your XML Web service directory.

  9. Returning to the project in Visual Studio, double-click the Web.config file in Solution Explorer.

  10. Add the following tag on the line after the <system.web> tag to configure integrated security for your XML Web service.

    <identity impersonate="true"/>

Expose the customersDataTable from Your Business Object

The next step in this walkthrough is to expose the dataset object you just created from your business object. This action makes the dataset available for Windows or Web applications to use.

Add methods to the XML Web service

  1. From the Build menu, choose Build Solution to build the solution.

  2. In Solution Explorer, double-click Service.vb to open the code editor.

  3. Add a line to the class body to create a new instance of the table adapter as shown below:

    Dim myAdapter As New DataSetTableAdapters.CustomersTableAdapter

    DataSetTableAdapters.CustomersTableAdapter myAdapter = new 
  4. Add a method named GetCustomers to deliver a datatable to the client.

    This method, shown below returns a datatable filled with the customer data.

    <WebMethod()> Public Function GetCustomers() As _
        Return myAdapter.GetData
    End Function 

    public DataSet.CustomersDataTable GetCustomers() 
        Return myAdapter.GetData();
  5. Add a method named UpdateCustomers to propagate changes from the client back to the database.

    This method, shown below, has an DataSet.CustomersDataTable parameter that contains the changed data and updates the database through the CustomersTableAdapter.Update method. The Update method accepts the changes in the dataset. The dataset is returned to the client. The client will then use this returned dataset to update its own instance of the Customers datatable. For information on the Update method and accepting changes in a dataset, see Introduction to Data Adapters.

    <WebMethod> Public Sub UpdateCustomers(ByVal CustomerChanges _
    As DataSet.CustomersDataTable)
    End Sub

    public void UpdateCustomers(DataSet.CustomersDataTable customerChanges)

    In a production application, you would add error checking and exception handling to these methods.

  6. From the File menu, choose Save All.

  7. From the Build menu, choose Build Solution.

In the previous sections, you have created a middle-tier business object that contains a dataset bound to a SQL Server database. You added code to the middle-tier CustomersWebService XML Web service to get data from a data source and update the data source with changes. The client accesses these functions through the GetCustomers and UpdateCustomers XML Web service methods.

Create the User Interface

After creating a middle-tier business object for data access and exposing it as an XML Web service, the next step is to create the client interfaces. There are two scenarios in this walkthrough: a traditional Windows Form and a Web Forms page. Both are created in this example as separate projects in the same solution. It is not necessary for you to create both interfaces.

Windows User Interface

A Windows interface uses the capabilities of the client computer to handle part of the application processing. Generally, a Windows interface provides greater functionality and a richer user experience than a Web-based interface. There is less of a load on the server than with a Web front end, because the server does not have to perform all of the application logic. Additionally, a Windows interface can take advantage of the resources available through the operating system, including calls to the file system and the registry.

The Windows application, consisting of one Windows Form, will contain a Web reference to CustomersWebService. The data in the database will be displayed in a DataGridView control when a Load button on the form is clicked. This load behavior is implemented by calling the XML Web service's GetCustomers method. The DataGridView control allows direct editing, with data changes passed directly to the underlying dataset. The form will also have a Save button. The code for this button will call the XML Web service's UpdateAuthors method to save the changes back to the database.

To create the Windows application

  1. On the File menu, point to Add then choose New Project, to open the Add New Project dialog box.

  2. Select Windows Application in the Visual Studio Installed Templates pane.

  3. Name the project CustomersWinClient and select a location for the project, then click OK.

    The CustomersWinClient project is added to the solution. Form1 is automatically added to the project and appears in the Windows Forms Designer.

  4. Add a Web reference to the ASP.NET Web Service project you created earlier:

    1. In Solution Explorer, right-click the CustomersWinClient project, and then click Add Web Reference on the shortcut menu.

    2. Click Web Services In This Solution then click CustomersService.

    3. Click Add Reference.

      You can now create an instance of the DataSet dataset in your application.

To add the controls to the form

  1. Drag a DataGridView control from the Data tab of the Toolbox onto the form.

  2. Drag a Button control from the Common Controls tab of the Toolbox onto the form. Set the button's Name property to LoadData and its Text property to Load.

  3. Drag another Button control from the Common Controls tab of the Toolbox onto the form. Set the button's Name property to SaveData and its Text property to Save.

  4. Double-click the form to open the Code Editor.

  5. Beneath the class declaration, create an instance of localhost.DataSet as shown below:

    Dim CustomerData As New localhost.DataSet.CustomersDataTable

    localhost.DataSet.CustomersDataTable CustomerData = new

To add code for the LoadData and SaveData buttons

  1. On the View menu, click Designer. Double-click the LoadData button to create an empty event handler for the Click event. XML Web service methods are called by first creating an instance of the service class and then calling the service methods. In this case, the GetCustomers method is called. The dataset returned is merged with the CustomerData dataset. The Credentials property of the XML Web service is used to pass your identity to the XML Web service, which in turn passes it on to the database server. Add the code shown below to the method.


    If the XML Web service is not running on your local computer, you will need to replace localhost in the code example with the name of the server running the XML Web service.

    Private Sub LoadData_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles LoadData.Click
        Dim ws As New localhost.Service()
        ws.Credentials = System.Net.CredentialCache.DefaultCredentials
        DataGridView1.DataSource = CustomerData
    End Sub

    private void LoadData_Click(object sender, System.EventArgs e)
        localhost.Service ws = 
            new localhost.Service();
        ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
        DataGridView1.DataSource = CustomerData;
  2. On the View menu, click Designer. Double-click the SaveData button to create an empty event handler for the Click event.


    In a production application, you would want to consider data concurrency issues in this step. For more information, see Introduction to Data Concurrency in ADO.NET.

    Add the following code to the methods:

    Private Sub SaveData_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SaveData.Click
        If CustomerData.HasChanges Then
           Dim ws As New localhost.Service()
           ws.Credentials = System.Net.CredentialCache.DefaultCredentials
        End If
    End Sub

    private void SaveData_Click(object sender, System.EventArgs e)
        if (CustomerData.HasChanges())
            localhost.Service ws = 
                new localhost.CustomersService();
            ws.Credentials = 

To run the application

  1. From the File menu, choose Save All.

  2. Select CustomersWinClient in Solution Explorer, right-click, and choose Set as StartUp Project.

  3. Press CTRL+F5 to run the application.

    A window is displayed that contains an empty DataGridView.

  4. Click Load to populate the table, make some changes, and then click Save to save changes you make.

In the preceding section, you added a Windows Forms project to your solution to act as a Windows interface. You connected the Windows Form to the XML Web service you created in the first section and used DataGridView and Button controls to create the user interface for loading and updating the data.

At this point you can deploy your application or, if you want, give the program additional functionality. A couple of suggestions are given below.

  • Deploy the Solution

    You could deploy your Web application to a server or create a setup project for your Windows application. For information about deploying your Windows application see Walkthrough: Deploying a Windows-based Application.

  • Support Internet Web Access

    This walkthrough handles authentication for intranet applications. Your application may be accessed by users over the Internet, so you might need a different authentication solution. For more information, see Using IIS Authentication with ASP.NET Impersonation. You might also investigate creating a web interface for your application in combination with web access protocols.

  • Consider Security

    This walkthrough uses Integrated Windows authentication to control access to the database. Database security is only one concern for Web applications. For a discussion of Web application security, see Basic Security Practices for Web Applications (Visual Studio).