Get Cities by Country Using Web Service Without DataBase

Background

Sometimes there is a need to get all the major cities of each country so most of the time its very difficult to manage such a type of huge database. To avoid this problem there is a free web service we can use to do it.

Let us see step-by-step how to do this.

Step 1: Locate Web Service

The following is the web service URL from which you can get the major cities by country name:

http://www.webservicex.net/globalweather.asmx?WSDL

Step 2: Create the web application

To use a Web Service at the client side we need to create a web application or another type of application such as Windows, console and so on but in this example we will create the web application.

Let us create the web application as:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Web" - "C#" - "Empty Web Site " (to avoid adding a master page).
  3. Provide the Web Site name such as "GetcitiesByCountryUsingWebService" or another as you wish and specify the location.
  4. Then right-click on the Solution Explorer and select "Add New Item" - "Default.aspx" page.
  5. Then add one button, one label and a GridView to the page

The source of the preceding web site will look like as:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>article By vithal wadje</title>  
  7. </head>  
  8. <body style="background-color: #C0C0C0">  
  9.     <form id="form1" runat="server">  
  10.     <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">  
  11.         Article by vithal wadje</h2>  
  12.     <br />  
  13.     <table style="margin-left: 30Px">  
  14.         <tr>  
  15.             <th>  
  16.                 Enter Country  
  17.             </th>  
  18.             <td>  
  19.                 <asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>  
  20.             </td>  
  21.         </tr>  
  22.         <tr>  
  23.             <td>  
  24.             </td>  
  25.             <td>  
  26.             </td>  
  27.         </tr>  
  28.         <tr>  
  29.             <td>  
  30.             </td>  
  31.             <td>  
  32.                 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="PageIndexChanging"  
  33.                     CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999"  
  34.                     BorderStyle="None" BorderWidth="1px">  
  35.                     <AlternatingRowStyle BackColor="#DCDCDC" />  
  36.                     <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  37.                     <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  38.                     <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  39.                     <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  40.                     <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  41.                     <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  42.                     <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  43.                     <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  44.                     <SortedDescendingHeaderStyle BackColor="#000065" />  
  45.                 </asp:GridView>  
  46.             </td>  
  47.         </tr>  
  48.         <tfoot>  
  49.             <tr>  
  50.                 <td>  
  51.                 </td>  
  52.                 <td>  
  53.                     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  54.                 </td>  
  55.             </tr>  
  56.         </tfoot>  
  57.     </table>  
  58.     </form>  
  59. </body>  
  60. </html> 
The user interface of the preceding source code will be as follows:

In the preceding screen if a valid country is entered then on the change event of the TextBox it will display all the major cities of the given country.

Step 3: Consume Web Service in the web application

Now the next step is to consume the GetCitiesByCountry web service in our web application. The following is the URL of the GetCitiesByCountry Web services.

http://www.webservicex.net/globalweather.asmx?WSDL

Adding a Web Service Reference to the ASP.NET Web Application

The most important task when consuming a Web Service in any application is to add the Web Service reference into the application. So how to add it? Let us see the procedure.

  1. Right-click on the ASP.NET Web Application and click on "Add Service Reference" as in the following:

    Adding Reference
     
  2. After clicking on the Add Web Reference tab, it will show the following window. Now this is a very important step, when adding the web reference to the ASP.NET Web Application. Since you see the "URL" option in the following window, on that window we need to paste in or type the Web Service URL address.

Our GetCitiesByCountry Web service URL is http://www.webservicex.net/globalweather.asmx?WSDL now we will paste it into the following window and after pasting it click on the forward arrow that will discover the web service class that is in our Web Service.

 
 
In the preceding service the GlobalWeatther is a class, now at the following of the preceding window there is a namespace TextBox that is the reference name of our service. By using that we can access the class and methods of the service, the reference name can be anything. I will provide a meaningful name, GetCitiesByCountryServiceRef. Now click on the OK button then after that the service reference will be added into the Solution Explorer and will look such as follows:
 
 

Now open the Default.aspx Page code behind of our web application and create the object of the web service class as:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Xml;  
  9. using System.Xml.Serialization;  
  10. using System.Text;  
  11. using System.Drawing;  
  12.   
  13. public partial class _Default : System.Web.UI.Page  
  14. {  
  15.     DataSet ds;  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.          
  19.     }  
  20.   
  21.     private string GetCitiesByCountry(string CountryName) //taking the input from User  
  22.     {  
  23.         //Creating the object of web service GlobalWeather class with the help of service Reference   
  24.         GetCitiesByCountryServiceRef.GlobalWeather obj = new GetCitiesByCountryServiceRef.GlobalWeather();    
  25.   
  26.         return obj.GetCitiesByCountry(CountryName); //ing country name input to Service  
  27.     }  
  28.     protected void TextBox1_TextChanged(object sender, EventArgs e)  
  29.     {  
  30.   
  31.         // calling GetCities() method on the change event of textbox  
  32.         GetCities();   
  33.   
  34.     }  
  35.   
  36.     private void GetCities()  
  37.     {  
  38.         //ensuring whether the country Name is not empty  
  39.         if (TextBox1.Text == "")  
  40.         {  
  41.              
  42.             Label1.Text = "Please Enter Country Name";  
  43.         }  
  44.         else  
  45.         {  
  46.             //getting the service Response to string   
  47.             string Data = GetCitiesByCountry(TextBox1.Text);  
  48.   
  49.             //converting the string Respsonse to XML   
  50.             XmlTextReader xtr = new XmlTextReader(new System.IO.StringReader(Data));  
  51.             ds = new DataSet();  
  52.             ds.ReadXml(xtr);  
  53.             if (ds.Tables.Count >=1 && ds.Tables[0].Rows.Count >= 1)  
  54.             {  
  55.                 //Binding Gridview  
  56.                 GridView1.DataSource = ds.Tables[0];  
  57.                 GridView1.DataBind();  
  58.                 Label1.Text = ds.Tables[0].Rows.Count.ToString() + "    Records Found";  
  59.             }  
  60.             else  
  61.             {  
  62.                 Label1.ForeColor = Color.Red;  
  63.                 Label1.Text = "Please Check the Spell of Entered Country Name   "+TextBox1.Text;  
  64.               
  65.             }  
  66.           
  67.         }  
  68.   
  69.   
  70.     }  
  71.     protected void PageIndexChanging(object sender, GridViewPageEventArgs e)  
  72.     {  
  73.         GridView1.PageIndex = e.NewPageIndex;  
  74.         GetCities();  
  75.   
  76.     }  

Now run the application, the following page will be displayed:
 
 
 
Now enter an incorrect country name, it will display the following message:
 
 
 
Now enter the correct country name, such as India; it will display the following list of major cities:
 
 
 
Now Enter Japan, it will display the following cities:
 
 
 
Now enter China, it will display the following cities:
 
 

Similarly you can see the cities of any valid country name.

In the preceding examples, you saw how to get the major city names of any country without a database using a free web service.

Notes:

  • For detailed code please download the Zip file attached above.
  • Also refer to my previous article about creating a Web Service.

Summary

I hope that beginners as well as students understand how to get the major cities of any country using a free web service without any database. I hope its useful for all of you, if you have any suggestion regarding this article then please contact me. Student suggestions are also welcomed.

Up Next
    Ebook Download
    View all
    Learn
    View all