Tuesday, June 19, 2012

City, State and Country usingcascading dropdownlist(through Webservice)


ASP.Net Page:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
Country:
<asp:DropDownList ID="ddlCountry" CssClass="ddlist" Width="202" runat="server">
</asp:DropDownList>
<asp:CascadingDropDown ID="CountryCascading" runat="server" Category="Country"  TargetControlID="ddlCountry"
PromptText="Select Country" ServiceMethod="BindCountrydropdown" LoadingText="Loading Countries..."
ServicePath="~/Assets/DropdownWebService.asmx">
</asp:CascadingDropDown>
State:
<asp:DropDownList ID="ddlState" CssClass="ddlist" Width="202" runat="server">
</asp:DropDownList>
<asp:CascadingDropDown ID="StateCascading" runat="server" Category="State" TargetControlID="ddlState"
ParentControlID="ddlCountry" LoadingText="Loading States..." PromptText="Select State"
ServiceMethod="BindStatedropdown" ServicePath="~/Assets/DropdownWebService.asmx">
</asp:CascadingDropDown>
City:
<asp:DropDownList ID="ddlCity" CssClass="ddlist" Width="202" runat="server">
</asp:DropDownList>
<asp:CascadingDropDown ID="RegionCascading" runat="server" Category="Region" TargetControlID="ddlCity"
ParentControlID="ddlState" LoadingText="Loading Cities..." PromptText="Select City"
ServiceMethod="BindRegiondropdown" ServicePath="~/Assets/DropdownWebService.asmx">
</asp:CascadingDropDown>

.CS File

if(Convert.ToInt32(objCompanyDT.Rows[0]["CountryID"])!=0)
{
  CountryCascading.SelectedValue = objCompanyDT.Rows[0]["CountryID"].ToString();
}

if (ddlCountry.SelectedItem.Text != "Select Country" && ddlCountry.SelectedItem.Text != "")
{
  objCompany.Country = ddlCountry.SelectedItem.Value;
}

In Web Service File:

using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Configuration;
using System.Data;
using IUSCRMLib.DataManagers;
namespace IUSCRM.Assets
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService()]
public class DropdownWebService : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] BindCountrydropdown(string knownCategoryValues, string category)
{
  SqlConnection concountry = IUSCRMLib.SqlHelper.GetDBConnetion();
  SqlCommand cmdcountry = new SqlCommand("select * from Countries where IsActive=1",  concountry);
  SqlDataAdapter dacountry = new SqlDataAdapter(cmdcountry);
  cmdcountry.ExecuteNonQuery();
  DataSet dscountry = new DataSet();
  dacountry.Fill(dscountry);
  List<CascadingDropDownNameValue> countrydetails = new List<CascadingDropDownNameValue>();
  foreach (DataRow dtrow in dscountry.Tables[0].Rows)
  {
    string CountryID = dtrow["CountryId"].ToString();
    string CountryName = dtrow["Country"].ToString();
    countrydetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
  }
  return countrydetails.ToArray();
 }
[WebMethod]
public CascadingDropDownNameValue[] BindStatedropdown(string knownCategoryValues, string category)
{
  int CountryID;
  StringDictionary countrydetails =  AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
  CountryID = Convert.ToInt32(countrydetails["Country"]);
  SqlConnection constate = IUSCRMLib.SqlHelper.GetDBConnetion();
  SqlCommand cmdstate = new SqlCommand("select * from State where CountryID=@CountryID and  State <> (select Country from Countries where CountryId=@CountryID) and IsActive=1", constate);
  cmdstate.Parameters.AddWithValue("@CountryID", CountryID);
  cmdstate.ExecuteNonQuery();
  SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
  DataSet dsstate = new DataSet();
  dastate.Fill(dsstate);
  List<CascadingDropDownNameValue> statedetails = new List<CascadingDropDownNameValue>();
  foreach (DataRow dtstaterow in dsstate.Tables[0].Rows)
  {
     string stateID = dtstaterow["StateID"].ToString();
     string statename = dtstaterow["State"].ToString();
     statedetails.Add(new CascadingDropDownNameValue(statename, stateID));
  }
  return statedetails.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] BindRegiondropdown(string knownCategoryValues, string category)
{
  int stateID;
  StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
  stateID = Convert.ToInt32(statedetails["State"]);
  SqlConnection conregion = IUSCRMLib.SqlHelper.GetDBConnetion();
  SqlCommand cmdregion = new SqlCommand("Select * from Cities where StateID=@StateID and City <> (select state from state where StateID=@StateID) and IsActive=1 ", conregion);
  cmdregion.Parameters.AddWithValue("@StateID", stateID);
  cmdregion.ExecuteNonQuery();
  SqlDataAdapter daregion = new SqlDataAdapter(cmdregion);
  DataSet dsregion = new DataSet();
  daregion.Fill(dsregion);
  List<CascadingDropDownNameValue> regiondetails = new List<CascadingDropDownNameValue>();
  foreach (DataRow dtregionrow in dsregion.Tables[0].Rows)
  {
     string regionID = dtregionrow["CityId"].ToString();
     string regionname = dtregionrow["City"].ToString();
     regiondetails.Add(new CascadingDropDownNameValue(regionname, regionID));
  }
  return regiondetails.ToArray();
}
}
}

1 comment:

  1. i need this code in windows form with the help of ADO query...

    ReplyDelete