Coloring Between the Lines

My 3 year old daughter, Daphne, showed me a drawing she had worked on. I was quite impressed on how well she colored between the lines.

I just had to compare notes, (you know, to try and show her up), so I dug up my old scrap book my mother put together for me when I was 4 years old in Nursery School.

Although I received a gold star for my master piece, I was now much more impressed on how well my daughter colored between the lines. 🙂

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>