Xceedas

Xceedas
xceedas

Tuesday 23 September 2014

Add Countries in Your DropDownList Using WebService

Here is the code to add countries in your DropDownList using WebService.

Here I am using a webservice from http://www.webservicex.net and a link to the webservice used is http://www.webservicex.net/country.asmx.

In the .aspx page put a 
DropDownList as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
    <asp:DropDownList ID="drpCNT" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Now to add the reference of your webservice, do following 
procedure:
  1. Open Solution Explorer, select your website and right-click and select "Add Web Reference"
  2. Now the following window will be opened, enter "http://www.webservicex.net/country.asmx" in the URL area that is circled in the image and press Enter.

    Country-DropDownList-1.jpg
  3. Now change Web reference name (optional) and click the "Add Reference" Button (circled in the image).

    Country-DropDownList-2.jpg

    After that it will create the following structure in Solution Explorer.

    Country-DropDownList-3.jpg
     
  4. Now put the following code in the .aspx.cs file (explained in code):
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Text;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                //get reference of your webservice
                myservice.country ct = new myservice.country();

                // str is an XML String which will hold all the countries in xml format
                string str = ct.GetCountries();

                // add first item in dropdownlist
                drpCNT.Items.Add("-Select-");

                //Create an XML Document and load your XML
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(str);

                //Get your nodes, here our node in Table because webservice will give us following format
                // http://www.webservicex.net/country.asmx/GetCountries
                XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Table");

                //Iterates for xml nodes and add them in dropdownlist
                foreach (XmlNode node in nodes)
                {
                    drpCNT.Items.Add(node["Name"].InnerText);
                }
            }

        }
    } 
     
  5. Finally you will get all the countries in your DropDownList.

No comments :