Simple Web Service Method for Generating Country, State and City in Dropdown List

Introduction

In this code snippet, I have shared the real time code which was working fine in my project to fill the country state and city based on the dropdown list items selected . Attached the source code in attachment section.

Aspx code

  1. <table>  
  2.     <tr>  
  3.         <td>  
  4.             SelectCountry  
  5.         </td>  
  6.         <td>  
  7.   
  8.             <asp:DropDownList ID="ddlCountry" runat="server" Width="100px">  
  9.             </asp:DropDownList>  
  10.             <ap:CascadingDropDown ID="CountryCascading" runat="server" Category="Country" TargetControlID="ddlCountry" LoadingText="Loading Countries..." PromptText="Select Country" ServiceMethod="BindCountry" ServicePath="~/Address_WebService.asmx">  
  11.             </ap:CascadingDropDown>  
  12.   
  13.         </td>  
  14.     </tr>  
  15.     <tr>  
  16.         <td>  
  17.             SelectState  
  18.         </td>  
  19.         <td>  
  20.             <asp:DropDownList ID="ddlState" runat="server" Width="100px">  
  21.             </asp:DropDownList>  
  22.             <ap:CascadingDropDown ID="StateCascading" runat="server" Category="State" TargetControlID="ddlState" ParentControlID="ddlCountry" LoadingText="Loading States..." PromptText="Select State" ServiceMethod="BindState" ServicePath="~/Address_WebService.asmx"></ap:CascadingDropDown>  
  23.   
  24.         </td>  
  25.     </tr>  
  26.     <tr>  
  27.         <td>  
  28.             SelectCity  
  29.         </td>  
  30.         <td>  
  31.             <asp:DropDownList ID="ddlCity" runat="server" Width="100px">  
  32.             </asp:DropDownList>  
  33.   
  34.             <ap:CascadingDropDown ID="CityCascading" runat="server" Category="Region" TargetControlID="ddlCity" ParentControlID="ddlState" LoadingText="Loading Cities..." PromptText="selectCity" ServiceMethod="BindCity" ServicePath="~/Address_WebService.asmx"> </ap:CascadingDropDown>  
  35.   
  36.             -  
  37.         </td>  
  38.   
  39.     </tr>  
  40. </table>  
Web service methods, 
  1. [WebService(Namespace = "http://tempuri.org/")]  
  2. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  3. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  4. [System.Web.Script.Services.ScriptService]  
  5. public class Address_WebService: System.Web.Services.WebService  
  6. {  
  7.     SqlConnection conn = new SqlConnection(OneStopMethods_Common.constring_Property);  
  8.     [WebMethod]  
  9.     public CascadingDropDownNameValue[] BindCountry(string knownCategoryValues, string category)  
  10.     {  
  11.         DataSet ds = new DataSet();  
  12.         conn.Open();  
  13.         SqlCommand cmd = new SqlCommand("select * from SelectCountry", conn);  
  14.         SqlDataAdapter adp = new SqlDataAdapter(cmd);  
  15.         cmd.ExecuteNonQuery();  
  16.         adp.Fill(ds);  
  17.         conn.Close();  
  18.         List < CascadingDropDownNameValue > CountryDetails = new List < CascadingDropDownNameValue > ();  
  19.         foreach(DataRow DR in ds.Tables[0].Rows)  
  20.         {  
  21.             string CountryID = DR["CountryId"].ToString();  
  22.             string CountryName = DR["County"].ToString();  
  23.             CountryDetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));  
  24.         }  
  25.         return CountryDetails.ToArray();  
  26.     }  
  27.     //Web method for bind state  
  28.     [WebMethod]  
  29.     public CascadingDropDownNameValue[] BindState(string knownCategoryValues, string category)  
  30.     {  
  31.         DataSet ds = new DataSet();  
  32.         int CountryID;  
  33.         StringDictionary CountryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);  
  34.         CountryID = Convert.ToInt32(CountryDetails["Country"]);  
  35.         conn.Open();  
  36.         SqlCommand cmd = new SqlCommand("select * from SelectCountrySelectState where CountryId=@CountryID", conn);  
  37.         cmd.Parameters.AddWithValue("@CountryId", CountryID);  
  38.         cmd.ExecuteNonQuery();  
  39.         SqlDataAdapter adp = new SqlDataAdapter(cmd);  
  40.         adp.Fill(ds);  
  41.         conn.Close();  
  42.         List < CascadingDropDownNameValue > StateDetails = new List < CascadingDropDownNameValue > ();  
  43.         foreach(DataRow DR in ds.Tables[0].Rows)  
  44.         {  
  45.             string stateID = DR["StateId"].ToString();  
  46.             string statename = DR["State"].ToString();  
  47.             StateDetails.Add(new CascadingDropDownNameValue(statename, stateID));  
  48.         }  
  49.         return StateDetails.ToArray();  
  50.     }  
  51.     //Web method for bind city  
  52.     [WebMethod]  
  53.     public CascadingDropDownNameValue[] BindCity(string knownCategoryValues, string category)  
  54.     {  
  55.         DataSet ds = new DataSet();  
  56.         int stateID;  
  57.         StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);  
  58.         stateID = Convert.ToInt32(statedetails["State"]);  
  59.         conn.Open();  
  60.         SqlCommand cmd = new SqlCommand("Select * from SelectStateSelectCity where StateId= " + stateID, conn);  
  61.         // cmd.Parameters.AddWithValue("@StateID", StateId)  
  62.         cmd.ExecuteNonQuery();  
  63.         SqlDataAdapter adp = new SqlDataAdapter(cmd);  
  64.         adp.Fill(ds);  
  65.         conn.Close();  
  66.         List < CascadingDropDownNameValue > CityDetails = new List < CascadingDropDownNameValue > ();  
  67.         foreach(DataRow DR in ds.Tables[0].Rows)  
  68.         {  
  69.             string CityID = DR["CityID"].ToString();  
  70.             string City = DR["City"].ToString();  
  71.             CityDetails.Add(new CascadingDropDownNameValue(City, CityID));  
  72.         }  
  73.         return CityDetails.ToArray();  
  74.     }  
  75. }