- <%@ 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>article By vithal wadje</title>
- </head>
- <body style="background-color: #C0C0C0">
- <form id="form1" runat="server">
- <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">
- Article by vithal wadje</h2>
- <br />
- <table style="margin-left: 30Px">
- <tr>
- <th>
- Enter Country
- </th>
- <td>
- <asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="PageIndexChanging"
- CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999"
- BorderStyle="None" BorderWidth="1px">
- <AlternatingRowStyle BackColor="#DCDCDC" />
- <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
- <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
- <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
- <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#F1F1F1" />
- <SortedAscendingHeaderStyle BackColor="#0000A9" />
- <SortedDescendingCellStyle BackColor="#CAC9C9" />
- <SortedDescendingHeaderStyle BackColor="#000065" />
- </asp:GridView>
- </td>
- </tr>
- <tfoot>
- <tr>
- <td>
- </td>
- <td>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- </td>
- </tr>
- </tfoot>
- </table>
- </form>
- </body>
- </html>
- 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 - 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) //taking the input from User
- {
- //Creating the object of web service GlobalWeather class with the help of service Reference
- GetCitiesByCountryServiceRef.GlobalWeather obj = new GetCitiesByCountryServiceRef.GlobalWeather();
- return obj.GetCitiesByCountry(CountryName); //passing country name input to Service
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- // calling GetCities() method on the change event of textbox
- GetCities();
- }
- private void GetCities()
- {
- //ensuring whether the country Name is not empty
- if (TextBox1.Text == "")
- {
- Label1.Text = "Please Enter Country Name";
- }
- else
- {
- //getting the service Response to string
- string Data = GetCitiesByCountry(TextBox1.Text);
- //converting the string Respsonse to XML
- 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)
- {
- //Binding Gridview
- 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();
- }
- }
Tuesday, 23 September 2014
Get Cities by Country Using Web Service Without DataBase
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment