Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Microsoft Visual Studio – ASP.Net Wrox Professional ASP.NET: In C# and vb – Chapter 19 CascadingDropDown Sample Causing Method Error 500

When debugging, the actual error states:

Unable to cast object of type ‘System.Object[]’ to type ‘AjaxControlToolkit.CascadingDropDownNameValue[]’.

After doing much research on this issue, it appears everyone’s resolution was to ensure that you included the ‘<WebMethod(), System.Web.Script.Services.ScriptMethod()> _’ above the public shared function that builds the ‘states’ for the first dropdown list (The sample in the Wrox book already has this in the code)… among other things that just didn’t do the trick.

I noticed there were many people who still complained that they tried everything suggested but to no avail.

After trial and error, I got mine working by simply altering the logic in the Function:

Instead of:

Public Shared Function GetStates(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 Return New Object() {New CascadingDropDownNameValue("Missouri", "Missouri"), New CascadingDropDownNameValue("Oregon", "Oregon")}
 End Function

do this instead:

Public Shared Function GetStates(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 GetStates = Nothing
 GetStates = {New CascadingDropDownNameValue("Missouri", "Missouri"), New CascadingDropDownNameValue("Oregon", "Oregon")}
 Return GetStates
 End Function

In other words, instead of ‘Return a New Object()….’, use the Function name of GetStates instead and then ‘Return GetStates’. This solves the casting problem and the Method Error 500.

Do the same for the GetCounties function.

Here is the full code from the book.

Before:

<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="AjaxControlToolkit" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <script runat="server" language="vb">
 <WebMethod(), System.Web.Script.Services.ScriptMethod()> _
 Public Shared Function GetStates(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 Return New Object() {New CascadingDropDownNameValue("Missouri", "Missouri"), New CascadingDropDownNameValue("Oregon", "Oregon")}
 End Function

 <WebMethod(), System.Web.Script.Services.ScriptMethod()> _
 Public Shared Function GetCounties(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 If knownCategoryValues.Contains("Missouri") Then
 Return New Object() {New CascadingDropDownNameValue("St. Charles", "St. Charles"), New CascadingDropDownNameValue("St. Louis", "St. Louis"), New CascadingDropDownNameValue("Jefferson", "Jefferson"), New CascadingDropDownNameValue("Warren", "Warren"), New CascadingDropDownNameValue("Franklin", "Franklin")}
 End If
 If knownCategoryValues.Contains("Oregon") Then
 Return New Object() {New CascadingDropDownNameValue("Baker", "Baker"), New CascadingDropDownNameValue("Benton", "Benton"), New CascadingDropDownNameValue("Clackamas", "Clackamas"), New CascadingDropDownNameValue("Clatsop", "Clatsop"), New CascadingDropDownNameValue("Columbia", "Columbia")}
 End If
 Return Nothing
 End Function
 </script>
 <title>CascadingDropDown</title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ToolkitScriptManager runat="server" ID="scriptManager" />
 <div>
 <asp:DropDownList runat="server" ID="ddl1" Width="200" />
 <br />
 <asp:DropDownList runat="server" ID="ddl2" Width="200" />
 <br />
 <asp:CascadingDropDown runat="server" ID="cdd1" TargetControlID="ddl1" PromptText="Select a State"
 Category="state" LoadingText="[Loading States]" ServiceMethod="GetStates" />
 <asp:CascadingDropDown runat="server" ID="cdd2" TargetControlID="ddl2" ParentControlID="ddl1"
 PromptText="Select County" Category="county" LoadingText="[Loading Counties]"
 ServiceMethod="GetCounties" />
 </div>
 </form>
</body>
</html>

After:

<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="AjaxControlToolkit" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <script runat="server" language="vb">
 <WebMethod(), System.Web.Script.Services.ScriptMethod()> _
 Public Shared Function GetStates(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 GetStates = Nothing
 GetStates = {New CascadingDropDownNameValue("Missouri", "Missouri"), New CascadingDropDownNameValue("Oregon", "Oregon")}
 Return GetStates
 End Function

 <WebMethod(), System.Web.Script.Services.ScriptMethod()> _
 Public Shared Function GetCounties(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 GetCounties = Nothing
 If knownCategoryValues.Contains("Missouri") Then
 GetCounties = {New CascadingDropDownNameValue("St. Charles", "St. Charles"), New CascadingDropDownNameValue("St. Louis", "St. Louis"), New CascadingDropDownNameValue("Jefferson", "Jefferson"), New CascadingDropDownNameValue("Warren", "Warren"), New CascadingDropDownNameValue("Franklin", "Franklin")}
 End If
 If knownCategoryValues.Contains("Oregon") Then
 GetCounties = {New CascadingDropDownNameValue("Baker", "Baker"), New CascadingDropDownNameValue("Benton", "Benton"), New CascadingDropDownNameValue("Clackamas", "Clackamas"), New CascadingDropDownNameValue("Clatsop", "Clatsop"), New CascadingDropDownNameValue("Columbia", "Columbia")}
 End If
 Return GetCounties
 End Function
 </script>
 <title>CascadingDropDown</title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ToolkitScriptManager runat="server" ID="scriptManager" />
 <div>
 <asp:DropDownList runat="server" ID="ddl1" Width="200" />
 <br />
 <asp:DropDownList runat="server" ID="ddl2" Width="200" />
 <br />
 <asp:CascadingDropDown runat="server" ID="cdd1" TargetControlID="ddl1" PromptText="Select a State"
 Category="state" LoadingText="[Loading States]" ServiceMethod="GetStates" />
 <asp:CascadingDropDown runat="server" ID="cdd2" TargetControlID="ddl2" ParentControlID="ddl1"
 PromptText="Select County" Category="county" LoadingText="[Loading Counties]"
 ServiceMethod="GetCounties" />
 </div>
 </form>
</body>
</html>

 

Leave a Reply