Click here to Skip to main content
14,979,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i am facing problem from three days.please solve this.i am getting values in my first dropdown list but when i select second dropdown list i get "Method Error500".while debugging i see problem is in stringdictionary variable.but iam not able to solve this.CountryId value is not coming in this variable.please see my problem.Also some Disassembly message is thrown when i reach to second method.what is this?my code is:
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDown.aspx.cs" Inherits="CascadingDropDown" EnableEventValidation="false" %>]]>
<![CDATA[<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>]]>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:toolkitscriptmanager id="ToolkitScriptManager1" runat="server" xmlns:asp="#unknown">
       
        </asp:toolkitscriptmanager>
        
        Country:
        <asp:dropdownlist id="ddlcountry" runat="server" xmlns:asp="#unknown">
             AutoPostBack="True">
        </asp:dropdownlist>
        <br />
        <br />
        State:
        <asp:dropdownlist id="ddlstate" runat="server" xmlns:asp="#unknown">
            onselectedindexchanged="ddlstate_SelectedIndexChanged">
        </asp:dropdownlist>
        
        <br />
        <asp:cascadingdropdown id="CascadingDropDown1" runat="server" xmlns:asp="#unknown">
            TargetControlID="ddlcountry" Category="country" 
            PromptText="select country" ServiceMethod="GetDropDownContents" 
            UseContextKey="True">
        </asp:cascadingdropdown>
        <br />
        
        <asp:cascadingdropdown id="CascadingDropDown2" runat="server" category="state" xmlns:asp="#unknown">
            ParentControlID="ddlcountry" PromptText="select state" 
            ServiceMethod="GetDropDownContents2" TargetControlID="ddlstate" 
            UseContextKey="True">
        </asp:cascadingdropdown>
        <asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional" xmlns:asp="#unknown">
        <contenttemplate>
        <asp:label id="Label1" runat="server" text="Label"></asp:label>
        </contenttemplate>
        <triggers>
        <asp:asyncpostbacktrigger controlid="ddlstate" eventname="SelectedIndexChanged" />
        </triggers>
        </asp:updatepanel>
        
       
    </div><asp:label id="Label2" runat="server" text="Label" xmlns:asp="#unknown"></asp:label>
    </form>
</body>
</html>


CODEBEHIND:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Services;
public partial class CascadingDropDown : System.Web.UI.Page
{  
    
   
    protected void Page_Load(object sender, EventArgs e)
    {
    }
  
        [System.Web.Services.WebMethodAttribute(),
        System.Web.Script.Services.ScriptMethodAttribute()]
        public static AjaxControlToolkit.CascadingDropDownNameValue[]
            GetDropDownContents(string knownCategoryValues, string category)
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
 sqlcon.Open();
 DataTable dt = new DataTable();
 SqlDataAdapter da = new SqlDataAdapter("select * from Country", sqlcon);
 da.Fill(dt);
List<ajaxcontroltoolkit.cascadingdropdownnamevalue> values = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int CountryId = (int)dt.Rows[i]["countryid"];
                string Countryname = (string)dt.Rows[i]["country"];
values.Add(new AjaxControlToolkit.CascadingDropDownNameValue(Countryname, CountryId.ToString()));
            }
return values.ToArray();
}
        
    
[System.Web.Services.WebMethodAttribute(),
        System.Web.Script.Services.ScriptMethodAttribute()]
        public static AjaxControlToolkit.CascadingDropDownNameValue[]
            GetDropDownContents2(string knownCategoryValues, string category)
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
 int CountryId;
StringDictionary country = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

            foreach (string key in country.Keys)
            {
                string value = country[key];
                CountryId = int.Parse(value.ToString());
               
                
            }
CountryId = Convert.ToInt32(country.ToString());
            sqlcon.Open();
            DataTable dt1 = new DataTable();
            SqlDataAdapter da1 = new SqlDataAdapter(" select * from State where countryid = @CountryId", sqlcon);
            da1.Fill(dt1);
            List<ajaxcontroltoolkit.cascadingdropdownnamevalue> statevalues = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                int stateId = (int)dt1.Rows[i]["stateid"];
             string statename = (string)dt1.Rows[i]["state"];
             statevalues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(statename.ToString(), stateId.ToString()));
            }
    
            return statevalues.ToArray();
    }
           
        //return default(AjaxControlToolkit.CascadingDropDownNameValue[]);
        protected void  ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = string.Format("you selected{0} from {1} category.", ddlstate.SelectedItem.Text, ddlcountry.SelectedItem.Text); 
}

</ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue>
Posted

Could you please check if the StringDictionary country, has value before this statement.

CountryId = Convert.ToInt32(country.ToString());

Inorder to get the value selected by the user you have to use

country["state"] instead of country.ToString() in the below statement.
CountryId = Convert.ToInt32(country.ToString());
   
hi, as u said,i tried that way also,but getting the same error.my code behind page code is like:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Services;
public partial class CascadingDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [System.Web.Services.WebMethodAttribute(),
    System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.CascadingDropDownNameValue[]
        GetDropDownContents(string knownCategoryValues, string category)
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
        sqlcon.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("select * from Country", sqlcon);
        da.Fill(dt);
        List values = new List();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int CountryId = (int)dt.Rows[i]["countryid"];
            string Countryname = (string)dt.Rows[i]["country"];
            values.Add(new AjaxControlToolkit.CascadingDropDownNameValue(Countryname, CountryId.ToString()));
        }
        return values.ToArray();
    }
    
[System.Web.Services.WebMethodAttribute(),
        System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.CascadingDropDownNameValue[]
        GetDropDownContents2(string knownCategoryValues, string category)
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
        int CountryId;
        StringDictionary categoryvalues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        CountryId = Convert.ToInt32(categoryvalues["state"]);        //CountryId = Convert.ToInt32(categoryvalues["country"]);
        sqlcon.Open();
        DataTable dt1 = new DataTable();
        SqlDataAdapter da1 = new SqlDataAdapter(" select * from State where countryid = @ CountryId", sqlcon);
        da1.Fill(dt1);
        sqlcon.Close();
        List statevalues = new List();
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            int stateid = (int)dt1.Rows[i]["stateid"];
            string statename = (string)dt1.Rows[i]["state"];
            statevalues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(stateid.ToString(), statename.ToString()));
        }
        return statevalues.ToArray();
   
Comments
Toli Cuturicu 5-Jul-10 16:37pm
   
Reason for my vote of 1
fake answer

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