Enroll Now!!! and get 10% special Discount on all courses. Limited Time only!!!

How To Create and Use an ASP.NET Web Service in C#

Last updated on Tue 17 Mar 2020



In this Blog Post we will show you how to connect your application to a web service using the Data Source Configuration Wizard. This will allow you to run queries from your application through Microsoft’s Live Search web service. Any data returned by the service will be displayed on your Windows Form that you will create.

First, you will need to obtain a free AppID from http://www.bing.com/developer/.

Copy and paste this link into your browser and then locate the “Get an AppID” on the bing.com page and follow the steps. To begin, you will need to create a new project. File > New > Project > Windows Forms Application and name it WebServTut > OK. Notice that the project is created in the Solution Explorer.

Next, we will need to connect to the service.

We can do this by running the Data Source Configuration Wizard. From the data menu simply click “Show Data Source”. Then from the “Data Source” window, select “Add New Data Source”. Then you’re prompted with a window.

  • Choose > Service then click next and type http://soap.search.msn.com/webservices.asmx?wsdl in the URL box and click Go.
  • Once the Web service is found, change the namespace to “LiveSearchService” > click OK.

***These configurations are only for demonstration purposes only. Different web services expose different functionality.

Here is the typical process for consuming data from a service:

  • First create an instance of the service,
  • then call methods exposed by the service.
  • Then drag the items from the Data Source window.
  • From there they should contain a BindingSource component.
  • Simply set the properties to the data returned by the service.

Now we want to create a DataGridView to display the data returned by the service.

Now within the Data Sources window, expand the SearchResponse node and then expand the Response node, then drag over the results node onto the form. Notice how DataGridView, BindingSource, and BindingNavigator are added to the form.

Now select the resultsDataGridView that you added to the form and locate the properties window.

From there select Columns and click the ellipses(..) to open the Edit Columns Dialog Box. Select the URL column and set the ColumnType property to DataGridViewLinkColumn and AutoSizeMode property to AllCells. Now let’s remove all columns except for Title, Description, URL and click OK.

Now we need to add controls for entering search criteria and running the search query.

To add a text box and button to the form simply right click the grayed disk icon on the form tool strip and select insert > Textbox. Locate the properties window and set the name property to tbxSearchCriteria and set the text to “Search”.

Next add a button the same way and name it btnSearch and set the DisplayStyle property to text and the Text property to “Search”.

Now we need to create an event handler for the CellContentClick event.

In order to create an event handler to open the web site when clicked in the grid we first need to select the resultsdataGridview on the form and click the Events button in the properties window. Double-click the CellContentClick event to create and navigate to the handler stub and add this code below:





private void resultsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)


// When the content in a cell is clicked check to see if it is the Url column.

// If it is, pass the url to the Process.Start method to open the web page.

if (resultsDataGridView.Columns[e.ColumnIndex].DataPropertyName == "Url")






This code checks which column was clicked and to navigate to the web page if the URL column is checked.

Lastly, we need to add code in order to access the Live Search Service and run a search query.

We can access this service by instantiating an instance of the service in your application and calling the methods exposed by the service. Open Form1 in the code editor and add the following method to it:





private void RunSearchRequest()


// Create an instance of the service.

LiveSearchService.MSNSearchPortTypeClient searchService = new LiveSearchService.MSNSearchPortTypeClient();

// Instantiate a new search request.

LiveSearchService.SearchRequest searchRequest = new LiveSearchService.SearchRequest();

// Create a new SourceRequest.

LiveSearchService.SourceRequest[] sourceRequest = new LiveSearchService.SourceRequest[1]; sourceRequest[0] = new LiveSearchService.SourceRequest();

// Set the number of results to return.

sourceRequest[0].Count = 7;

// To search the web, set the SourceType to Web.

sourceRequest[0].Source = LiveSearchService.SourceType.Web; // Set the columns to be returned from the search query. sourceRequest[0].ResultFields = LiveSearchService.ResultFieldMask.Description | LiveSearchService.ResultFieldMask.Url | LiveSearchService.ResultFieldMask.Title;

// Set the search query to the value in the text box.

searchRequest.Query = tbxSearchCriteria.Text;

// Set the search request to the array of source requests.

searchRequest.Requests = sourceRequest;

// ***REPLACE THIS WITH A VALID AppID. Obtain a free AppID at:

// http://search.live.com/developer

searchRequest.AppID = "D2F4847D41D19CB4E172983074FE4FC50BC8F855"; searchRequest.CultureInfo = "en-US";

// Create a SearchResponse, then call the search method

// and assign the return value to the response object.

LiveSearchService.SearchResponse searchResponse = searchService.Search(searchRequest);

// Bind the results from the search query to the form's BindingSource.

resultsBindingSource.DataSource = searchResponse.Responses[0].Results;



***Make sure to replace the searchrequest.AppID = ‘AppID” with the AppID value that you obtained from the live search service.

Open Form1 and Double-click the search button and add this code to create an event handler to run a search when the search button is clicked:









private void btnSearch_Click(object sender, EventArgs e)





Save and run the application. The Grid displays first 10 search results.

Drop us a Query

+91 97846 54326

Available 24x7 for your queries