Xceedas

Xceedas
xceedas

Tuesday, 23 September 2014

Get Cities by Country Using Web Service Without DataBase

  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> 
  61.  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
  62. 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); //passing 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.     }  
    77. }  

No comments :