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 following illustration describes the architecture of the application.

Architecture of the distributed application you will create

The application you will build is a simple data application with lookup and edit. You will build both a Windows and Web-based client to display the Authors table from the SQL Server Pubs sample database. For the Web portion, you will use the Web Forms Designer to create a Web page that is compatible with a standard HTML 3.2 browser. On the server, the Web Forms code will call an XML Web service to retrieve data that contains Authors information from the database. For the Windows portion, you will build a Windows application that will communicate with this same XML Web service to retrieve a dataset containing the author information. Communication with the XML Web service is handled using HTTP and XML.

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. For more information on other security models and architectures, see Access Permissions for Web Applications.

System Requirements

In order to complete this walkthrough, you will need:

  • Access to a server with the Pubs SQL Server sample database, configured for Integrated Windows authentication. The Pubs 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 Introduction to Data Access with ADO.NET.

Process for Creating a Distributed Application

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 Pubs 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
    2. Create a Web user interface
  3. Deploy the solution or add more features

Create the Middle-Tier Business Object

The business object you create 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 ASP.NET Web Service project template is a Web-based project designed for creating middle-tier components that expose their interfaces as XML Web services. For more information, see ASP.NET Web Service Projects in Visual Studio.

The XML Web service will expose two methods. The first, GetAuthors, will return a dataset from the database. The second, UpdateAuthors, will update the database with changes from the user. Several private members are created to implement these methods. They include a dataset definition, a data adapter, a data connection, and several command objects to retrieve data from and update the database. The following diagram describes the XML Web service.

The middle tier in the distributed application

To create an ASP.NET Web Service project

  1. On the File menu, point to New, then click Project to display the New Project dialog box.
  2. Depending on what language you want to use, select Visual Basic Projects or Visual C# Projects in the Project Types pane, and then select ASP.NET Web Service in the 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/AuthorsWebService, and then click OK.
    Tip   Because the Web server is on your computer, you can use the server name LOCALHOST.
    Tip   If you have trouble creating the ASP.NET Web Service project, see Web Access Failed Dialog Box.
  4. The AuthorsWebService project is added to the solution. The Component Designer for Service1.asmx appears in the development environment.
  5. In Solution Explorer, double-click Service1.asmx to select it.
  6. In the Properties window, set the Name property of Service1 to AuthorsService.
  7. In Solution Explorer, right-click the Service1.asmx file, select Rename, and rename the file AuthorsService.asmx, to match the service name.

    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 two objects to the XML Web service: a SqlDataAdapter object and a SqlConnection object. The connection object creates a new connection to the database while the data adapter queries the database and feeds the results into a DataSet object. The data adapter also updates the database with changes made to the dataset by the user. Using the connection to the database, a dataset will be created to hold an instance of the information present in the database. The dataset will be used later in the walkthrough to display data in the Windows Form and the Web Forms page. For an overview, see Introduction to Distributed Applications and Data Integration.

To create a database connection and data adapter

  1. Click Server Explorer.
    Note   By default, Server Explorer appears as a vertical tab on the left of the development environment. If the tab is not visible, click the View menu and choose Server Explorer.
  2. In Server Explorer, right-click the Data Connections node and choose Add Connection from the shortcut menu.
  3. In the Connection tab of the Data Link Properties dialog box, enter the name of the SQL Server where the pubs database is installed. If you have SQL Server on the local computer, enter (local).
  4. Select Use Windows NT Integrated security for the logon information.
    Note   If you do not have integrated security set up on your system, see your network administrator.
  5. Select the pubs database from the list.
  6. Click Test Connection to validate the information you provided, and then click OK to establish the connection.

    A new node appears in the Data Connections node of Server Explorer.

    Note   If the database connection fails, see your database administrator.
  7. In Server Explorer, expand the node for the new connection node and then expand the Tables node.
  8. Find the authors node and expand it to show the fields in the authors table.
  9. Using CTRL+Click, select the au_id, au_lname, au_fname, and city fields.
  10. Drag these fields from Server Explorer onto the design surface. A SqlConnection object paired with a SqlDataAdapter object appears in the designer. A connection has now been created to the database, with the transfer of information to be handled by the SqlDataAdapter object. These components are configured to move a dataset with the authors table in and out of the database.
    Note   This walkthrough uses the SqlDataAdapter object, which is optimized for working with SQL Server 7.0 or later. You could use the OleDbDataAdapter object because it is more generic, providing ADO.NET access to any OLE DB-compatible data source. If you were to connect to a database that is not a SQL Server database, then an OleDbDataAdapter object and an OleDbConnection object would be created by the project system.
    Note   In this walkthrough, you have populated the dataset with some of the columns and all of the rows from the authors table. In production applications, you typically optimize data access by creating a query that returns only the columns and rows you need. For an example, see Walkthrough: Displaying Data in a Windows Form Using a Parameterized Query.

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. For more information, see Security Model.

To configure Integrated Windows authentication

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.

  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 AuthorsWebService 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"/>
    

To create a dataset class definition

  1. In Solution Explorer, double-click the AuthorsService file to open it in the designer.
  2. From the Data menu choose Generate DataSet. In the Generate Dataset dialog box, select New and name the dataset "authors1". Do not check the box labeled "Add this dataset to the designer."
    Note   A dataset schema file, authors1.xsd, is created and added to the project. This schema file contains a class definition for authors1. This class, which inherits from the DataSet class, contains a typed dataset definition for the authors table.
  3. From the File menu, choose Save All.

Expose the authors1 DataSet 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. In Solution Explorer, double-click AuthorsService if it is not already open in the designer.
  2. On the View menu, click Code.
  3. Add a method named GetAuthors to deliver a dataset to the client.

    This method, shown below, creates a new authors1 dataset and fills it using the SqlDataAdapter object based on the authors table. The method then returns the dataset.

    ' Visual Basic
    <WebMethod> Public Function GetAuthors() As authors1
       Dim authors As New authors1()
       SqlDataAdapter1.Fill(authors)
       Return authors
    End Function
    
    // C#
    [WebMethod]
    public authors1 GetAuthors() 
    {
       authors1 authors = new authors1();
       sqlDataAdapter1.Fill(authors);
       return authors;
    }
    
  4. Add a method named UpdateAuthors to propagate changes from the client back to the database.

    This method, shown below, has an authors1 dataset parameter (authorChanges) that contains the changed data and updates the database through the SqlDataAdapter.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 authors1 dataset. For information on the Update method and accepting changes in a dataset, see Introduction to Data Adapters.

    ' Visual Basic
    <WebMethod> Public Function UpdateAuthors(ByVal authorChanges _
    As authors1) As authors1
       If Not (authorChanges Is Nothing) Then
          SqlDataAdapter1.Update(authorChanges)         
          Return authorChanges
       Else
          Return Nothing
       End If
    End Function
    
    // C#
    [WebMethod]
    public authors1 UpdateAuthors(authors1 authorChanges)
    {
       if (authorChanges != null)
       {
          sqlDataAdapter1.Update(authorChanges);
          return authorChanges;
       }
       else
       {
          return null;
       }
    }
    
    Note   In a production application, you would add error checking and exception handling to these methods.
  5. From the File menu, choose Save All.
  6. 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 AuthorsWebService XML Web service to get data from a data source and update the data source with changes. The client accesses these functions through the GetAuthors and UpdateAuthors 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.

Windows User Interface Option

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 following diagram highlights the client you will implement.

Windows client in the distributed application

The Windows application, consisting of one Windows Form, will contain a Web reference to AuthorsWebService. The data in the database will be displayed in a DataGrid control when a Load button on the form is clicked. This load behavior is implemented by calling the XML Web service's GetAuthors method. The DataGrid 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 Project, then click New Project to open the Add New Project dialog box.
  2. Depending on what language you want to use, select Visual C# Projects or Visual Basic Projects in the Project Types pane, then select Windows Application in the Templates pane.
  3. Name the project AuthorsWinClient and select a location for the project.

    The AuthorsWinClient 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 AuthorsWinClient project, and then click Add Web Reference on the shortcut menu.
    2. Enter the location of your XML Web service project (http://ServerName/AuthorsWebService/AuthorsService.asmx) in the URL box at the top of the Add Web Reference dialog box, then press ENTER. If you have trouble adding the Web reference, see Add Web Reference Dialog Box and Adding and Removing Web References.
    3. Click Add Reference.
    4. You can now create an instance of the authors1 dataset in your application.

To add the controls to the form

  1. Drag a DataGrid control from the Windows Forms tab of the Toolbox onto the form.
  2. Drag a Button control from the Windows Forms 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 Windows Forms tab of the Toolbox onto the form. Set the button's Name property to SaveData and its Text property to Save.
  4. Drag a DataSet object from the Data tab of the Toolbox onto the form. The Add DataSet dialog box appears. Select Typed dataset and choose "AuthorsWinClient.ServerName.authors1" from the Name list. This action creates a DataSet object in the component tray that is based on the authors1 dataset class definition.
  5. Select the DataSet control and set the Name property to AuthorData.
  6. Select the DataGrid control and select AuthorData from the DataSource property list. Select authors from the DataMember property list. The column headings of the DataGrid are set to the authors table column names.

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 GetAuthors method is called. The dataset returned is merged with the AuthorData 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.
    Note   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.
    ' Visual Basic
    Private Sub LoadData_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles LoadData.Click
       Dim ws As New AuthorsWinClient.localhost.AuthorsService()
       ws.Credentials = System.Net.CredentialCache.DefaultCredentials
       AuthorData.Merge(ws.GetAuthors())
    End Sub
    
    // C#
    private void LoadData_Click(object sender, System.EventArgs e)
    {
       AuthorsWinClient.localhost.AuthorsService ws = 
          new AuthorsWinClient.localhost.AuthorsService();
       ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
       AuthorData.Merge(ws.GetAuthors());
    }
    
  2. On the View menu, click Designer. Double-click the SaveData button to create an empty event handler for the Click event.

    If there are changes in the dataset, a new dataset of type authors1 is created to hold just the changed data. This dataset is then passed to the XML Web service's UpdateAuthors method. The dataset is returned with the changes accepted, and the AuthorData dataset is updated to reflect these new changes. For more information about accepting changes in a dataset, see Introduction to Data Adapters.

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

    ' Visual Basic
    Private Sub SaveData_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SaveData.Click
       If AuthorData.HasChanges() Then
          Dim ws As New AuthorsWinClient.localhost.AuthorsService()
          ws.Credentials = System.Net.CredentialCache.DefaultCredentials
          Dim diffAuthors As New AuthorsWinClient.localhost.authors1()
          diffAuthors.Merge(AuthorData.GetChanges())
          diffAuthors = ws.UpdateAuthors(diffAuthors)
          AuthorData.Merge(diffAuthors)
       End If
    End Sub
    
    // C#
    private void SaveData_Click(object sender, System.EventArgs e)
    {
       if (AuthorData.HasChanges())
       {
          AuthorsWinClient.localhost.AuthorsService ws = 
             new AuthorsWinClient.localhost.AuthorsService();
          ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
          AuthorsWinClient.localhost.authors1 diffAuthors 
             = new AuthorsWinClient.localhost.authors1();
          diffAuthors.Merge(AuthorData.GetChanges());
          diffAuthors = ws.UpdateAuthors(diffAuthors);
          AuthorData.Merge(diffAuthors);
       }
    }
    

To run the application

  1. From the File menu, choose Save All.
  2. Select AuthorsWinClient 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 table with headers from the authors table in the pubs database.

  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 DataGrid and Button controls to create the user interface for loading and updating the data. In the next section, you will create a Web-based user interface for the solution.

Web User Interface Option

A Web interface helps your application reach a broad variety of client computers and browsers. All of the user-interface processing is accomplished on the Web server rather than the client. Using a Web Forms page, you will create a Web interface that accesses the same middle-tier business object as does the Windows interface that you created in the previous section. The following diagram highlights the client you will implement.

Web Client in the distributed application

To create the Web Forms application

  1. On the File menu, point to Add Project, then click New Project.
  2. In the Add New Project dialog box, select Visual C# Projects or Visual Basic Projects in the Project Types pane, then click ASP.NET Web Application in the 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/AuthorsWebClient, and then click OK.

    A new project is added to Solution Explorer. A Web Forms page named WebForm1.aspx is added to the project and is loaded into the designer.

  4. Add a Web reference to the ASP.NET Web Service project you created earlier:
    1. In Solution Explorer, right-click the AuthorsWebClient project, and click Add Web Reference on the shortcut menu.
    2. Enter the location of your XML Web service project (http://ServerName/AuthorsWebService/AuthorsService.asmx) in the URL box at the top of the Add Web Reference dialog box, then press ENTER.
    3. Click Add Reference.
    4. You can now create an instance of the authors1 dataset in your application.

To add controls to the Web page

  1. Drag a DataSet object from the Data tab of the Toolbox onto the form. The Choose a DataSet dialog box appears. Select TypedDataSet and choose "AuthorsWebClient.ServerName.authors1" from the Name list. A DataSet object is added to the component tray.
  2. Select the DataSet object and set the Name property to AuthorData.
  3. Drag a DataGrid control from the Web Forms tab of the Toolbox onto the form.
  4. In the Properties window for the DataGrid control, set the DataSource property to AuthorData and the DataMember property to authors. These settings appear in the drop-down lists for these controls. The column headings of the DataGrid are set to the column names of the authors table.
  5. To support in-place editing in the DataGrid control, you need to add an Edit, Update, Cancel column, which will contain an Edit button. When the user clicks the Edit button, the contents of the row will be displayed in text boxes, and the Edit button will be replaced by Update and Cancel buttons. To add this column:
    1. Click the Property Builder link at the bottom of the Properties window, then select the Columns tab in the dialog box.
    2. Expand the Button Column item in the Available Columns pane.
    3. Choose Edit, Update, Cancel and click the add () button .
    4. Click OK.

To add code for the Edit, Update, and Cancel buttons

  1. Right-click the form and select View Code. Add code to the Page_Load event to fill the DataGrid control, as shown below. This code creates an instance of the XML Web service, fills the AuthorData dataset, and binds the dataset to the DataGrid control. Each time the page makes a round trip back to the user, the grid will contain a fresh copy of the data from the database. 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.
    Note   In this walkthrough, a new copy of the authors table is retrieved from the database each time the Web Forms page is downloaded to the client browser. This may not provide optimal performance. For a discussion of other methods for optimizing performance, see Web Data Access Strategy Recommendations.
    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       Dim ws As New AuthorsWebClient.localhost.AuthorsService()
       ws.Credentials = System.Net.CredentialCache.DefaultCredentials
       AuthorData.Merge(ws.GetAuthors())
       If Not Page.IsPostBack Then
          Me.DataGrid1.DataBind()
       End If
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       AuthorsWebClient.localhost.AuthorsService ws = 
          new AuthorsWebClient.localhost.AuthorsService();
       ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
       AuthorData.Merge(ws.GetAuthors());
       if (! Page.IsPostBack) 
       {
          DataGrid1.DataBind();
       }
    }
    
  2. When a user clicks the Edit button, the EditCommand event of the DataGrid control is raised. Use this event to change the EditItemIndex property of the DataGrid control. The row specified by the EditItemIndex property will be displayed with all the data in text boxes.
    1. Create an event handler for the EditCommand event.

      In Visual Basic, right-click the page and select View Code. In the left drop-down box of the Code Editor, select DataGrid1. In the right drop-down box, select EditCommand.

      -or-

      In Visual C#, click the DataGrid1 control. In the Properties window, click the Events button to display the list of DataGrid events. Double-click the EditCommand event.

    2. Add the code as shown below.
      ' Visual Basic
      Private Sub DataGrid1_EditCommand(ByVal source As Object, _
      ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
      Handles DataGrid1.EditCommand
         DataGrid1.EditItemIndex = e.Item.ItemIndex
         DataGrid1.DataBind()
      End Sub
      
      // C#
      private void DataGrid1_EditCommand(object source, 
      System.Web.UI.WebControls.DataGridCommandEventArgs e)
      {
         DataGrid1.EditItemIndex = e.Item.ItemIndex;
         DataGrid1.DataBind();
      }
      
  3. Create an event handler for the CancelCommand event. (See step 2 for adding event handlers.)

    When the user clicks the Cancel button, the CancelCommand event of the DataGrid control is raised. Use this event to set the EditItemIndex property to -1 so that the current row is displayed as text again. Add the code as shown below.

    ' Visual Basic
    Private Sub DataGrid1_CancelCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles DataGrid1.CancelCommand
       DataGrid1.EditItemIndex = -1
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void DataGrid1_CancelCommand(object source, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       DataGrid1.EditItemIndex = -1;
       DataGrid1.DataBind();
    }
    
  4. When a user clicks the Update button, the UpdateCommand event of the DataGrid control is raised. This method is responsible for updating the AuthorData dataset with the changes from the DataGrid control and propagating these changes back to the database through the XML Web service. Create an event handler for the UpdateCommand event. (See step 2 for adding event handlers.) Add the code as shown below.
    ' Visual Basic
    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles DataGrid1.UpdateCommand
       ' update each of the fields in the dataset row
       Dim i As Integer
       For i = 1 To AuthorData.authors.Columns.Count
          ' Controls(0) returns a Control object, so it must be cast 
          ' to TextBox.
          Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
          Dim row As DataRow = AuthorData.authors(e.Item.DataSetIndex)
          row(AuthorData.authors.Columns(i - 1).Caption) = t.Text
       Next
       ' Update the database.
       If Me.AuthorData.HasChanges() Then
          Dim ws As New AuthorsWebClient.localhost.AuthorsService()
          ws.Credentials = System.Net.CredentialCache.DefaultCredentials
          Dim diffAuthors As New AuthorsWebClient.localhost.authors1()
          diffAuthors.Merge(Me.AuthorData.GetChanges())
          ws.UpdateAuthors(diffAuthors)
          AuthorData.Merge(diffAuthors)
       End If
       ' Take the row out of edit mode and display the new data.
       DataGrid1.EditItemIndex = -1
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void DataGrid1_UpdateCommand(object source, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       // Change the data in the dataset.
       for (int i=1; i <= AuthorData.authors.Columns.Count; i++) 
       {
          TextBox t = (TextBox)(e.Item.Cells[i].Controls[0]);
          DataRow row = AuthorData.authors[e.Item.DataSetIndex];
          row[AuthorData.authors.Columns[i-1].Caption] = t.Text;
       }
       
       // Update the database.
       if (AuthorData.HasChanges()) 
       {
          AuthorsWebClient.localhost.AuthorsService ws = 
             new AuthorsWebClient.localhost.AuthorsService();
          ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
          AuthorsWebClient.localhost.authors1 diffAuthors = 
             new AuthorsWebClient.localhost.authors1();
          diffAuthors.Merge(AuthorData.GetChanges());
          ws.UpdateAuthors(diffAuthors);
          AuthorData.Merge(diffAuthors);
       }
       DataGrid1.EditItemIndex = -1;
       DataGrid1.DataBind();
    }
    

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. For more information, see Security Model.

To configure Integrated Windows authentication

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.

  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.) Expand the node for your server.
  2. Expand the Default Web Site node.
  3. Right-click the node for AuthorsWebClient and select Properties from the shortcut menu.
  4. Click the Directory Security tab.
  5. Click the Edit button in the Anonymous access and authentication control section.
  6. Clear the Anonymous Access check box.
  7. Check the Integrated Windows authentication box. You have now configured your XML Web service directory.
  8. Returning to the project in Visual Studio, double-click the Web.config file in Solution Explorer.
  9. Add the following tag on the line after the <system.web> tag to configure integrated security for your XML Web service.
    <identity impersonate="true"/>
    

To run the application

  1. Select AuthorsWebClient in Solution Explorer, right-click, and choose Set as StartUp Project.
  2. Press CTRL+F5 to run the application.

In this section, you added an ASP.NET Web Application project to your solution to act as a browser interface to your data. You connected the Web Forms page to the XML Web service you created in the first section and used a DataGrid control to create the user interface for displaying and editing the data.

Deploy the Solution or Add More Features

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 Web application see Walkthrough: Deploying a Web Solution. For information about deploying your Windows application see Walkthrough: Deploying a Windows Application.

  • Support Internet Web Access

    This walkthrough handles authentication for intranet Web applications. Your application may be accessed by users over the Internet, so you might need a different authentication solution. For more information, see IIS Authentication and Access Permissions for Web Applications.

  • Support Addition or Deletion of Records

    This walkthrough only supports the display and update of records from the database, while you might also want to support adding and deleting records. Addition and deletion are not inherently supported in the DataGrid Web server control, though there is support in the Windows Forms DataGrid control. For more information, see Allowing Users to Delete Items in a DataGrid Web Server Control, and Updating, Inserting, and Deleting Records in a Dataset.

  • 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.

See Also

Programming the Web with XML Web Services | Web Forms Pages | Windows Forms | XML Web Services in Managed Code | Introduction to Distributed Applications and Data Integration | Getting Started With Enterprise Templates in Distributed Applications

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft