ASP.NET Logo Back to WebMatrix Home

 
XML Web Services
Data access WebMethod
In this walkthrough, you will create a WebMethod that returns a complex type; a DataSet representing rows in the Orders database table.
  1. Open the myFirstWebService.asmx file in Code view.

     
  2. From the Code Builders tab in the Toolbox, drag a SELECT Data Method Code Builder onto the XML Web Service page.

     
  3. You will see the Connect to Database dialog box appear.

     
  4. Type localhost in the Server box (default).

     
  5. Click the Database drop-down. ASP.NET Web Matrix will connect to the data source and populate the Database drop-down with database names. Select the Orders database that we created in an earlier section walkthrough.

     
  6. Click OK.

     
  7. You will see step one of the Code Builder Wizard appear; it is called the Construct SELECT Query dialog box.

    Select Orders Columns

  8. Select Orders in the Tables list view.

     
  9. Select the check box next to the * item in the Columns list view.

     
  10. Click Next

     
  11. You will see step two of the Code Builder Wizard appear; it is called the Query Preview dialog box.

     
  12. Click Test Query.

     
  13. You will see the Orders rows appear in the Query Preview dialog box results grid. 

    Test Query Results

  14. Click Next.

     
  15. You will see step three of the Code Builder Wizard appear; it is called the Enter A Caption dialog box. Type GetOrders into the method name box.

     
  16. Select the DataSet radio button (default).

     
  17. Click Finish.

    Enter Caption Dialog

  18. The Wizard will close and ASP.NET Web Matrix will place a function called GetOrders in Code view. The function accepts no input parameters and returns a DataSet as an output parameter. The DataSet is populated with all of the orders in the Orders table.

     
  19. Add a WebMethod attribute to the GetOrders function. The WebMethod accepts no input parameters and returns a DataSet as an output parameter. The DataSet is populated with all of the rows in the Orders table:

     

    <WebMethod> Function GetOrders() As System.Data.DataSet
            Dim connectionString As String = _
                "server='localhost'; trusted_connection=true; Database='Orders'"
                
            Dim sqlConnection As System.Data.SqlClient.SqlConnection = _
                New System.Data.SqlClient.SqlConnection(connectionString)
            
            Dim queryString As String = "SELECT [Orders].* FROM [Orders]"
            Dim sqlCommand As System.Data.SqlClient.SqlCommand = _
                New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
            
            Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = _
                New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
                
            Dim dataSet As System.Data.DataSet = _
                New System.Data.DataSet
                
            dataAdapter.Fill(dataSet)
            
            Return dataSet
        End Function
    
  20. Save and run the XML Web Service page.

     
  21. Your XML Web Service page will automatically appear in a new instance of your Web browser.

     
  22. You will see the XML Web service's HTML description file. The XML Web service's HTML description page shows you all the XML Web service methods supported by a particular XML Web service. 

    WS Desc File GetOrders

  23. Click the GetOrders link at the top of the HTML description file.

     
  24. Click Invoke

    Invoke GetOrders WebMethod

  25. The Web service will return an XML response that shows all of the rows in the Orders table in a new instance of your Web browser.

    GetOrders WebMethod Results

  26. Close the Web browser instance.

Next Step >>