Consume A Web Service In ASP.NET

Here, I have used some free web services  and consume it to show country, country code, currency and currency code of any country.

I have designed my form like the following screenshot:



And here is the HTML code for this.
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div style="margin-left:200px;border-style:solid; border-color:aqua; width:270px;height:300px;">  
  4.             <div>  
  5.                 Country:  
  6.             </div>  
  7.             <div>  
  8.                 <asp:DropDownList ID="ddl_country" runat="server" AutoPostBack="True" Height="21px" OnSelectedIndexChanged="ddl_country_SelectedIndexChanged" Width="250px"></asp:DropDownList>  
  9.             </div>  
  10.             <br />  
  11.             <div>  
  12.                 Country Code  
  13.             </div>  
  14.             <div>  
  15.                 <asp:TextBox runat="server" ID="txt_countrycode"></asp:TextBox>  
  16.             </div>  
  17.             <br />  
  18.             <div>  
  19.                 Currency;  
  20.             </div>  
  21.             <div>  
  22.                 <asp:TextBox runat="server" ID="txt_currency"></asp:TextBox>  
  23.             </div>  
  24.             <br />  
  25.             <div>  
  26.                 CurrencyCode:  
  27.             </div>  
  28.             <div>  
  29.                 <asp:TextBox runat="server" ID="txt_isd"></asp:TextBox>  
  30.             </div>  
  31.         </div>  
  32.     </form>  
  33. </body> 
Now in this project I am going to add the service Reference.
 


You can get the web service in this link.

Now go to the webservice page and copy the link for getcountry. Now add this link in web service reference.



Give an appropriate Namespace and search the link by clicking the GO button.

 
 


Then click ok.

Now here I am writing the code by using the namespace to get all country name and binding them in the dropdownlist.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         fillcountry();  
  6.     }  
  7. }  
  8. public void fillcountry()  
  9. {  
  10.     myservice.countrySoapClient obj = new myservice.countrySoapClient(); //you can declare it globally.  
  11.     var country = obj.GetCountries();  
  12.     StringReader stream = null;  
  13.     XmlTextReader reader = null;  
  14.     DataSet ds = new DataSet();  
  15.     stream = new StringReader(country);  
  16.     // Load the XmlTextReader from the stream  
  17.     reader = new XmlTextReader(stream);  
  18.     ds.ReadXml(reader);  
  19.     ddl_country.DataSource = ds;  
  20.     ddl_country.DataTextField = "Name";  
  21.     ddl_country.DataBind();  

Now it will bind all the countries to the dropdown list.


Now change the AutoPostBack property to true.
 


And write to get the country code, currency, currency code, etc in ddl_country_SelectedIndexChanged. Here is the code.
  1. protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e) {  
  2.     myservice.countrySoapClient obj = new myservice.countrySoapClient(); //you can declare it globally.  
  3.     string currency = obj.GetCurrencyByCountry(ddl_country.SelectedItem.Text);  
  4.     StringReader stream = null;  
  5.     XmlTextReader reader = null;  
  6.     DataSet ds = new DataSet();  
  7.     stream = new StringReader(currency);  
  8.     reader = new XmlTextReader(stream);  
  9.     ds.ReadXml(reader);  
  10.     txt_currency.Text = ds.Tables[0].Rows[0]["Currency"].ToString();  
  11.     txt_countrycode.Text = ds.Tables[0].Rows[0]["CountryCode"].ToString();  
  12.     txt_isd.Text = ds.Tables[0].Rows[0]["Currencycode"].ToString();  

Now run the project and choose any country from the DropDownList, you will get all the details.
 


Thus in this way we can consume web service in our project.

Up Next
    Ebook Download
    View all
    Learn
    View all