Share via


Lesson 2: Defining a Report Dataset for an ADO.NET DataSet from a Web Service

New: 17 July 2006

In this lesson you will create a data source connection to a Web Service that returns an XML System.Data.DataSet object. You can create and deploy a Web service that returns an XML System.Data.DataSet object or use an existing Web service available to you. You will define a query string that specifies the data you want to retrieve for a report dataset from the XML System.Data.DataSet returned by the Web service.

For this tutorial, a Web service retrieves employee and manager information calling the stored procedure uspGetEmployeeManagers in the AdventureWorks sample database. This stored procedure accepts an EmployeeID value and produces a result set that shows the employee-manager relationship recursively to the top-level employee.

Procedures

To create a Web service that returns an ADO.NET DataSet (Optional)

  1. In Microsoft Visual Studio, from the File menu, click New, then click Web Site.

    The New Web Site dialog box opens.

    Note

    If you do not see Web Site as an option, check the requirements section for Tutorial: Using XML Data in a Report.

  2. In the Templates pane, select ASP.NET Web Service.

  3. In the Location drop-down list, select HTTP.

  4. In the Language drop-down list, select Visual Basic or C#.

  5. Click the Browse button.

    The Choose Location dialog box opens.

  6. In the Open a Web site pane, verify the Local IIS button is selected.

  7. On the Web site tree control, navigate to the parent folder for this new Web site.

  8. Click the Create New Web Application button.

  9. Type in a name for your Web site.

  10. Click Open.

  11. Click OK.

    The App_Code/Service code page for the programming language you selected in step 4 opens in Visual Studio, showing the sample WebMethod HelloWorld.

    Note

    The default namespace is http://tempuri.org/. This namespace will be used in the query when you define a report dataset below. If you change this default, you will need to change the query.

  12. Add the following two namespace references to the top of the source file for Visual Basic or C#.

    using System.Data;
    using System.Data.SqlClient;
    
    Imports System.Data
    Imports System.Data.SqlClient
    
  13. Replace the HelloWorld WebMethod with the following Visual Basic or C# code.

    [WebMethod]
    public DataSet  GetEmployeeManagers(String EmployeeID)
    {
      Int32 val = Int32.Parse(EmployeeID);
      DataSet ds = new DataSet("Results");
      try
      {
        if (val < 1)
        val = 1;
        else if (val > 290) val = 290;
        SqlDataAdapter da = new 
    SqlDataAdapter(@"uspGetEmployeeManagers " + val.ToString(),
      @"Data Source=localhost; Initial Catalog=AdventureWorks; " +
      @"Integrated Security=true");
      da.Fill(ds);
      }
      catch (Exception e)
      {
        throw;
      }
      return ds;
     }
    
    <WebMethod()> _
    Public Function GetEmployeeManagers(ByVal EmployeeID As String) As DataSet
      Dim ds As DataSet = New DataSet("Results")
      Try
        Dim val As Integer = Integer.Parse(EmployeeID)
        If (val < 1) Then
           val = 1
        ElseIf (val > 290) Then
           val = 290
        End If
        Dim da As SqlDataAdapter = _
            New SqlDataAdapter("uspGetEmployeeManagers " + _
             val.ToString(), _
           "Data Source=localhost; Initial Catalog=AdventureWorks; " + _
                "Integrated Security=true")
            da.Fill(ds)
        Catch e As Exception
        Throw
      End Try
      Return ds
    End Function
    
  14. On the Build menu, click Build Web Site.

  15. On the Build menu, click Publish Web Site.

To test your published Web service (Optional)

  1. Open Internet Explorer.

  2. In the Address bar, type your Web service URL.

    For example, https://localhost/XMLTutorial/Service.asmx.

    The Web service default page appears.

  3. Click GetEmployeeManagersFromAdventureWorks.

  4. Type in an EmployeeID value between 1 and 290.

  5. Click Invoke.

    The Web service retrieves a dataset with the employee managers from the AdventureWorks database using the stored procedure uspGetEmployeeManagers.

Note

If you get the message "Execute permission denied on object", you need to grant execute permission on the AdventureWorks stored procedure for ASPNETUser, as described in the following procedure.

To grant execute permissions for the stored procedure (Optional)

  1. Open Microsoft SQL Server Management Studio.

  2. Connect to the Database Engine where the AdventureWorks sample database is installed.

  3. In Object Explorer, expand the Databases node, then the AdventureWorks node, then the Programmability node, and then the Stored Procedures node.

  4. Right-click dbo.uspGetEmployeeManagers, and choose Properties.

  5. In the Select a page pane, click Permissions.

  6. For the Users or roles pane, click the Add button.

  7. Click the Browse button. Select [ASPNETUser].

  8. Click OK.

    You have added permission for anyone running ASP.NET to execute the uspGetEmployeeManagers stored procedure.

To define a report dataset for the Web service

  1. Start Report Designer and open the report server project created for this tutorial.

  2. In Data view, select New Dataset. Type a name for the dataset (for example, XMLWebDataSet).

  3. In the Dataset dialog box, in Data source, select New Data Source. The Data Source dialog box appears.

  4. Type a name for the data source (for example, XMLWebDataSource).

  5. In Type, select XML.

  6. In Connection string, type the following URL to the Report Server Web service:

    https://localhost/XMLTutorial/Service.asmx

  7. In the Credentials tab, select Use Windows Authentication (Integrated Security).

  8. Click OK to save your changes and close the Data Source dialog box.

  9. On the Report menu, click Report Parameters. The Report Parameters dialog box opens.

  10. Click Add.

  11. In the Properties section, in the Name text box, type EmployeeID.

  12. In the Prompt text box, type Employee ID.

  13. Click OK.

  14. On the Data tab, click the Edit Selected Dataset (…) button on the toolbar. The Dataset dialog box opens.

  15. Click the Parameters tab.

  16. In the Name column, type EmployeeID.

  17. Click in the Value text box to enable the drop-down list, and select =Parameters!EmployeeID.Value. This maps the dataset parameter to the report parameter.

  18. Click OK.

  19. In the Dataset dialog box, type the following query using the namespace version information that you verified in step 1:

    <Query>
        <SoapAction>
            http://tempuri.org/GetEmployeeManagers
        </SoapAction>
        <Method Namespace="http://tempuri.org/" 
            Name="GetEmployeeManagers">
        </Method>
    </Query>
    
  20. Click Run (!) button on the toolbar. The Define Query Parameters dialog box opens.

  21. In the Parameter Value column, enter an EmployeeID number between 1 and 290.

  22. Click OK. The dataset is added to the Datasets window.

  23. Examine the result set. By default, a query extracts data from all XML elements and attributes down to the leaf node on the first XML path it finds in the dataset.

  24. Paste the following ElementPath into the Query pane just after the <Query> tag.

    <ElementPath IgnoreNamespaces="True">
        GetEmployeeManagersResponse {}/
        GetEmployeeManagersResult/diffgram{}/
        Results {}/Table 
    </ElementPath>
    

    This provides the query with an element path specifying which elements to use for retrieving data in the result set. In this case, the empty brackets ({}) after the nodes GetEmployeeManagersResponse, GetEmployeeManagersResult, diffgram, and Results, all instruct the query to skip data from these nodes. The Table node with no brackets instructs the query to retrieve all nodes and attributes for that node.

    Note

    To specify which Table elements to retrieve for the dataset fields collection, list them after the Table element inside curly brackets, as shown in the following example: Table {FirstName, LastName, EmployeeID, ManagerID, RecursionLevel}.

  25. Click Run (!) to view the result set. The changes made in the ElementPath element change the result set returned by the query.

  26. When you are satisfied with the result set, click the Refresh Fields (Refresh dataset fields) button on the toolbar. This saves the report definition and updates the view of fields in the Report Datasets window to show all the fields you can use.

Next Steps

You have successfully defined a report dataset from an XML System.Data.DataSet object returned by a Web service. When the report is processed, data from each XML Table element and its attributes are retrieved from the Web service. Next, you will create a report dataset from XML embedded in the report. See Lesson 3: Defining a Report Dataset from Embedded XML Data.

See Also

Concepts

Reporting Services Tutorials

Other Resources

Defining Report Datasets for XML Data
Connecting to a Data Source
Defining Report Datasets
How to: Create or Edit a Report-Specific Data Source (Report Designer)
How to: Create a Dataset (Report Designer)
Working With Fields in a Report Dataset
How to: Add, Edit, or Delete a Field in the Datasets Window (Report Designer)
Report Datasets (Report Designer)

Help and Information

Getting SQL Server 2005 Assistance