Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,
I am having problem in casecade dropdown .I have two dropdown one for country and another for State. Country dropdown binding but on change of country state dropdown in not binding.

All code of webservice and aspx page are given below.
Please suggest .

Aspx Page code:

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageInnerHTML.master" AutoEventWireup="true"
    CodeFile="CasecadeDrop.aspx.cs" Inherits="CasecadeDrop" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPart" runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table width="100%" align="center" style="height: 50px">
        <tr>
            <td align="right">
                Country:&nbsp;&nbsp;
            </td>
            <td align="left">
                &nbsp;&nbsp;<asp:DropDownList ID="ddlCountry" runat="server" Width="250px">
                </asp:DropDownList>
                <cc1:CascadingDropDown ID="ccddCountry" ServicePath="CasecadeDropdown.asmx" ServiceMethod="GetCountry"
                    Category="Country" runat="server" TargetControlID="ddlCountry" PromptText="Select Country"
                    LoadingText="Loading Countries..">
                </cc1:CascadingDropDown>
            </td>
        </tr>
        <tr>
            <td align="right">
                State:&nbsp;&nbsp;
            </td>
            <td align="left">
                &nbsp;&nbsp;
                <asp:DropDownList ID="ddlState" runat="server" Width="250px">
                </asp:DropDownList>
                <cc1:CascadingDropDown ID="ccddState" ServicePath="CasecadeDropdown.asmx" ServiceMethod="GetState"
                    Category="State" runat="server" TargetControlID="ddlState" PromptText="Select State">

                </cc1:CascadingDropDown>
            </td>
        </tr>
    </table>
</asp:Content>


Webservice code


C#
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Services;
using System.Web.Services.Protocols;
using AjaxControlToolkit;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

[System.Web.Script.Services.ScriptService ]
/// <summary>
/// Summary description for CasecadeDropdown
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class CasecadeDropdown : System.Web.Services.WebService 
{
    string Constr = ConfigurationManager.AppSettings["MyConnectionString"].ToString();

    public CasecadeDropdown ()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() 
    {
        return "Hello World";
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetCountry(string knownCategoryValues, string category)
    {
        int Qtype=1;
        SqlConnection connection = new SqlConnection(Constr);
        connection.Open();
        SqlCommand command = new SqlCommand("TempGetCountryStateNew", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@Qtype", SqlDbType.Int).Value = Qtype;
        SqlDataAdapter adap = new SqlDataAdapter(command);
        DataSet ds=new DataSet ();
        adap.Fill(ds);
        connection.Close();
         List<cascadingdropdownnamevalue> countrydetails = new List<cascadingdropdownnamevalue>();
         foreach(DataRow dtrow in ds.Tables[0].Rows)
           {
               string CountryID = dtrow["Country_Code"].ToString();
               string CountryName = dtrow["Country_Name"].ToString();
           countrydetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
           }
         return countrydetails.ToArray();
    }
   
    [WebMethod]
    public CascadingDropDownNameValue[] GetState(string knownCategoryValues, string category)
    {
        int Qtype = 2;
        int countryID;
       //This method will return a StringDictionary containing the name/value pairs of the currently selected values

        StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        countryID = Convert.ToInt32(countrydetails["Country"]);

        //StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        //countryID = Convert.ToString(countrydetails["Country"]);
        //StringDictionary kv =CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        //int DeptID;
        //if (!kv.ContainsKey("Country") || !Int32.TryParse(kv["Country"], out DeptID))
        //{
        //    return null;
        //}

        SqlConnection connection = new SqlConnection(Constr);
        connection.Open();
        SqlCommand command = new SqlCommand("TempGetCountryStateNew", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@Qtype", SqlDbType.Int).Value = Qtype;
        command.Parameters.Add("@CountryCode", SqlDbType.Int).Value = Convert .ToInt32(countryID);
        SqlDataAdapter adap = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        connection.Close();
        List<cascadingdropdownnamevalue> Statedetails = new List<cascadingdropdownnamevalue>();
        foreach (DataRow dtrow in ds.Tables[0].Rows)
        {
            string StateCode = dtrow["State_code"].ToString();
            string StateName = dtrow["State_name"].ToString();
            Statedetails.Add(new CascadingDropDownNameValue(StateName, StateCode));
        }
        return Statedetails.ToArray();
        
    }
}
Posted
Updated 8-May-12 20:13pm
v2
Comments
Ganesan Senthilvel 9-May-12 2:13am    
Have you set breakpoints? Can't it get hit on debug mode?

Hi Bhanu,

As per my understanding from the code provided, you are binding the data to the drop down when the service starts.But i am not able to find any method that is called when the dropdown value is changed.I mean no method is being called for that event.Am i missing something here?Please let me know..
 
Share this answer
 
C#
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CasecadeDropdown : System.Web.Services.WebService
{
    string Constr = ConfigurationManager.AppSettings["MyConnectionString"].ToString();

    public CasecadeDropdown ()
    {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

in your webservice you must to remove comment line from
C#
// [System.Web.Script.Services.ScriptService]
public class CasecadeDropdown : System.Web.Services.WebService
{


like this
C#
 [System.Web.Script.Services.ScriptService]
public class CasecadeDropdown : System.Web.Services.WebService
{


and pls add this to your state cascadedropdown
SQL
ParentControlID="ddlCountry"


and edit here

C#
List<cascadingdropdownnamevalue> countrydetails = new List<cascadingdropdownnamevalue>();


to

C#
List<CascadingDropDownNameValue> countrydetails = new List<CascadingDropDownNameValue>();


because of c# is a case sensitive language
 
Share this answer
 
v3
 
Share this answer
 
Database structure of above query

SQL
CREATE TABLE [dbo].[TempCountry](
    [CountryID] [int] IDENTITY(1,1) NOT NULL,
    [CountryName] [varchar](52) NULL
)
CREATE TABLE [dbo].[TempState](
    [StateID] [int] IDENTITY(1,1) NOT NULL,
    [StateName] [varchar](52) NULL,
    [CountryID] [int] NULL
)
 
Share this answer
 
Comments
manognya kota 9-May-12 2:50am    
Hi Bhanu,

If you are adding some information to your question, please use improve question option . Do not post it as solution.

-Manognya.K
Bhanu Pratap Verma 9-May-12 3:17am    
Thank sir. I will keep in mind.
Dear Sir,
GetState Method in webservice for get all state for selected country.
 
Share this answer
 
Dear Sir ,
Still I am getting same probelm.Country dropdown is binding but on change of country dropdown state dropdown not binding ,That why I posted my problem with all code.

And the line you suggest not allowed by .net compiler.
List<cascadingdropdownnamevalue> countrydetails = new List<cascadingdropdownnamevalue>();

Currect :

List<cascadingdropdownnamevalue> countrydetails = new List<cascadingdropdownnamevalue>();
 
Share this answer
 
v2
Comments
Sandeep Mewara 9-May-12 8:57am    
Looks like you wanted to respond to an answer. Use 'Have a question or Comment' at the end of that answer to talk to the answerer.
Sergey Alexandrovich Kryukov 9-May-13 2:26am    
This is outrageous. You even accepted it formally. I call it cheating.
—SA
Dear Sir ,
Still I am getting same probelm.Country dropdown is binding but on change of country dropdown state dropdown not binding ,That why I posted my problem with all code.
 
Share this answer
 
Comments
Dain Ucak 9-May-12 8:29am    
please read solution 4 clearly. i tested it and worked correctly.
Sergey Alexandrovich Kryukov 26-Jun-14 13:52pm    
Please stop posting fake "answers". This is the abuse.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900