Hello friends,
Here is the code to add Countries in your dropdownlist using WebService…
Here i am using a webservice from http://www.webservicex.net and link of used webservice is http://www.webservicex.net/country.asmx .
In .aspx page put a dropdown list
<%@ 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 runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ 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 steps…
1. open Solution explorer , select your website and right click and select the option Add Web Reference
2. Now following window will be opened , put http://www.webservicex.net/country.asmx in URL area that is circled in image.
3. Now chnage Web reference name (optional) and click Add Reference Button (circled in image)
4. Now put the following code in .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…