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:
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.
- Right-click on the ASP.NET Web Application and click on "Add Service Reference" as in the following:
- 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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Text;
- using System.Drawing;
-
- public partial class _Default : System.Web.UI.Page
- {
- DataSet ds;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- private string GetCitiesByCountry(string CountryName)
- {
-
- GetCitiesByCountryServiceRef.GlobalWeather obj = new GetCitiesByCountryServiceRef.GlobalWeather();
-
- return obj.GetCitiesByCountry(CountryName);
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
-
-
- GetCities();
-
- }
-
- private void GetCities()
- {
-
- if (TextBox1.Text == "")
- {
-
- Label1.Text = "Please Enter Country Name";
- }
- else
- {
-
- string Data = GetCitiesByCountry(TextBox1.Text);
-
-
- XmlTextReader xtr = new XmlTextReader(new System.IO.StringReader(Data));
- ds = new DataSet();
- ds.ReadXml(xtr);
- if (ds.Tables.Count >=1 && ds.Tables[0].Rows.Count >= 1)
- {
-
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- Label1.Text = ds.Tables[0].Rows.Count.ToString() + " Records Found";
- }
- else
- {
- Label1.ForeColor = Color.Red;
- Label1.Text = "Please Check the Spell of Entered Country Name "+TextBox1.Text;
-
- }
-
- }
-
-
- }
- protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView1.PageIndex = e.NewPageIndex;
- GetCities();
-
- }
- }
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.