Saturday, March 24, 2012

CascadingDropDownList going against SQLServer in VB

I tried to find an answer to this problem by searching through the site; however, I gave up after plowing through eleven pages returned from the forums, especially since there were 3,604 entries. Anyway, here goes...

I'm working on a website and want to use the CascadingDropDownList extender going against some tables in SQL Server 2005. I've found some C# code that'd do the trick, but my current employer uses only VB .Net, so I'm stuck with it. The problem occurs inside the Web Service function when I attempt to return the results from my Web Service to my form. The code is as follows:

<WebMethod()> _
PublicFunction GetEntityTypes(ByVal valuesAsString,ByVal idAsInteger)As AjaxControlToolkit.CascadingDropDownNameValueDim categoryValuesAsString() = values.Split(":"c, ";"c)
Dim entityIDAsInteger =CType(categoryValues(1),Integer)
Dim entitiesAs Generic.List(Of CascadingDropDownNameValue) =New Generic.List(Of CascadingDropDownNameValue)
Dim drAs SqlDataReader = SqlDataAccess.ExecuteReader(_connString, CommandType.StoredProcedure, _
"Entity.GetEntityTypes")

While dr.Read()
entities.Add(New CascadingDropDownNameValue(dr("EntityTypeID"), dr("EntityType")))
EndWhile

Return entities ' The problem occurs here regardless of whether I'm trying to return entities or entities.ToArray()

EndFunction

Has anyone done this in VB? If so, what am I doing wrong? If you have any code you could share, that'd also be greatly appreciated! Big Smile

What is the error that you get?


If I try just returning entities, I get:
Value of type 'System.Collections.Generic.List(Of AjaxControlToolkit.CascadingDropDownNameValue)' cannot be converted to 'AjaxControlToolkit.CascadingDropDownNameValue'.

If I try returning entities.ToArray(), I get:
Value of type '1-dimensional array of AjaxControlToolkit.CascadingDropDownNameValue' cannot be converted to 'AjaxControlToolkit.CascadingDropDownNameValue'.

From what I've seen in the C# version, they're creating a List<T> of CascadingDropDownNameValue, along the line of
List<CascadingDropDownNameValue> entities = new List<CascadingDropDownNameValue>()

thus my attempt to use the line
Dim entitiesAs Generic.List(Of CascadingDropDownNameValue) =New Generic.List(Of CascadingDropDownNameValue)

It looks like I've finally found the answer! I declared entities thusly:
Dim entites = new Generic.List(Of CascadingDropDownNameValue)()
and the project compiled. Whether it runs or not remains to be seen.

Nope, now I'm getting a System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[AjaxControlToolkit.CascadingDropDownNameValue]' to type 'AjaxControlToolkit.CascadingDropDownNameValue'.

I wish I could do this in C# since I've been using it the last three years after having programmed in VB.NET the previous two, but I can't since the company is using VB.NET on all of it's Web development.


The problem is in the declaration of the function. You are returning the right thing List<CascadingDropDownNameValue> (in C#) but this is not what you define in the function declaration.

You need to change it to as List(Of CascadingDropDownNameValue)

Public Function GetEntityTypes(ByVal valuesAs String,ByVal idAs Integer)As AjaxControlToolkit.CascadingDropDownNameValue

Becomes

Public Function GetEntityTypes(ByVal valuesAs String,ByVal idAs Integer)As List(Of AjaxControlToolkit.CascadingDropDownNameValue)

HTH,

Jonathan.

No comments:

Post a Comment