Search code examples
asp.netweb-servicesajaxcontroltoolkit

ajaxToolkit:CascadingDropDown got error


I want to create cascadingdropdown using ajaxtoolkit but having error.

since newer version of ajaxcontroltoolkit have remove ToolkitScriptManager, so i use scriptmanager

<ajaxToolkit:CascadingDropDown 
    ID="cdlOffice" 
    TargetControlID="DropDownList1" 
    PromptText="Select Office" 
    PromptValue="" 
    ServicePath="~/WebService1.asmx" 
    ServiceMethod="GetLocation" 
    runat="server" 
    Category="LocationId" 
    LoadingText="Loading..." />

<div class="form-group">
                            <asp:Label runat="server" AssociatedControlID="DropDownList1" CssClass="col-md-2 control-label">Location</asp:Label>
                            <div class="col-md-10">
                                <asp:DropDownList id="DropDownList1" runat="server" class="select2-me" data-placeholder="--SELECT--" data-rule-required="true" style="width:250px;" />
                            </div>
                        </div>

and WebService1.asmx as follow

  [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public CascadingDropDownNameValue[] GetLocation(string knownCategoryValues)
        {
            string query = "SELECT DISTINCT ITOFF_NAME, ITOFF_ID FROM ITOFF_TBL ORDER BY ITOFF_NAME";
            List<CascadingDropDownNameValue> Location = GetData(query);
            return Location.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetFloor(string knownCategoryValues)
        {
            string Location = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["LocationId"];
            string query = string.Format("SELECT ITOFF_LEVEL, ITOFF_ID FROM ITOFF_TBL WHERE ITOFF_NAME = {0}", Location);
            List<CascadingDropDownNameValue> Floor = GetData(query);
            return Floor.ToArray();
        }

        private List<CascadingDropDownNameValue> GetData(string query)
        {
            string conString = ConfigurationManager.ConnectionStrings["ITFORMConnectionString"].ConnectionString;
            SqlCommand cmd = new SqlCommand(query);
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                cmd.Connection = con;
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        values.Add(new CascadingDropDownNameValue
                        {
                            name = reader[0].ToString(),
                            value = reader[1].ToString()
                        });
                    }
                    reader.Close();
                    con.Close();
                    return values;
                }
            }
        }

    }

i'm having error showing

Unhandled exception at line 6, column 97862 in http://localhost:60461/Scripts/WebForms/MsAjax/MicrosoftAjax.js

0x800a138f - JavaScript runtime error: Unable to get property 'webServiceFailedNoMsg' of undefined or null reference

all the code i learn from googling. is there anything missing or did wrong?


Solution

  • Try removing the String Param 'knownCategoryValues' from you getLocation() web method definition:

     [WebMethod]
                public CascadingDropDownNameValue[] GetLocation()
                {
                    string query = "SELECT DISTINCT ITOFF_NAME, ITOFF_ID FROM ITOFF_TBL ORDER BY ITOFF_NAME";
                    List<CascadingDropDownNameValue> Location = GetData(query);
                    return Location.ToArray();
                }
    

    And remove locationID property from the from the Cascading DropDown definition.

    Or, if you are getting the locationID from another dropdown then leave it in and define it as the ParentControlID