Showing posts with label returned. Show all posts
Showing posts with label returned. Show all posts

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.

CascadingDropDownList - cant handle more than 765 items!

Hello,

My CascadingDropDownList gives me Method 500 error when more than 765 items are returned from the webservice! Is there a way to fix this? Thanks..

Hi,

that's probably related with the limit for the max size of the JSON payload set by ASP.NET AJAX. You can change it in the web.config:

<jsonSerialization maxJsonLength="500" />


this certainly worked! thank you!
Thanks! Helped me alot.
I have tried putting that in every section and it always errors out as unkown element, if someone can help I would really appreciate it!

Hi,

it must be added inside the configuration -> system.web.extensions -> scripting -> webServices section.


Thank you!

Hi,

I have changed the web config maxJsonLength to 1000. But i dont have that many values. First dropdown is giving me a value but on the second dropdown I am getting method error 500. My webservice code is as follows.

Imports

System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml

<WebService(Namespace:=

"http://tempuri.org/")> _
<System.Web.Script.Services.ScriptService()> _

Public

Class MIService
Inherits System.Web.Services.WebService
Shared _DocumentAs XmlDocument
Shared _lockAsNewObject
PublicReadOnlyProperty Document()As XmlDocument
Get
If (_DocumentIsNothing)Then
SyncLock _lock
_Document =New XmlDocument
_Document.Load(HttpContext.Current.Server.MapPath("~/App_Data/MIService.xml"))
EndSyncLock
EndIf
Document = _Document
ExitProperty
EndGet
EndProperty
PublicReadOnlyProperty Hierarchy()AsString()
Get
Dim _HeirarchyAsString() = {"city","zip"}
Return _Heirarchy
EndGet
EndProperty
<WebMethod()> _
PublicFunction GetDropDownContents(ByVal knownCategoryValuesAsString,ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()
'Get a dictionary of known category/value pairs
Dim knownCategoryValuesDictionaryAsNew StringDictionary
knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)' Perform a simple query against the data document
Return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category)
EndFunction
EndClass

My code for aspx file is:

<%

@.PageLanguage="VB"AutoEventWireup="true"CodeFile="Default.aspx.vb"Inherits="_Default"EnableEventValidation="False"%>
<%@.RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="cc1" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
<scriptrunat="server">
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
PublicSharedFunction GetDropDownContentsPageMethod(ByVal knownCategoryValuesAsString,ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()
ReturnNew MIService().GetDropDownContents(knownCategoryValues, category)EndFunction
</script>
</head>
<body>
<formid="form1"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<div>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:DropDownListID="DropDownList1"runat="server"Width="200px">
</asp:DropDownList><br/>
<asp:DropDownListID="DropDownList2"runat="server"Width="200px">
</asp:DropDownList><br/>
<asp:DropDownListID="DropDownList3"runat="server"AutoPostBack="True"Width="200px">
</asp:DropDownList><br/>
<br/>
<asp:LabelID="Label1"runat="server"Width="224px"></asp:Label><br/>
<br/>
<cc1:CascadingDropDownID="CascadingDropDown1"runat="server"Category="city"LoadingText="[loading City...]"PromptText="Please select a city."ServiceMethod="GetDropDownContents"ServicePath="MIService.asmx"TargetControlID="DropDownList1"></cc1:CascadingDropDown><cc1:CascadingDropDownID="CascadingDropDown2"runat="server"Category="zip"LoadingText="[Loading Zip...]"ParentControlID="DropDownList1"PromptText="Please enter zip."ServiceMethod="GetDropDownContentPageMethod"TargetControlID="DropDownList2"></cc1:CascadingDropDown><cc1:CascadingDropDownID="CascadingDropDown3"runat="server"Category="county"LoadingText="[loading County..]"ParentControlID="DropDownList2"PromptText="Please select county."ServiceMethod="GetDropDownContents"ServicePath="MIService.asmx"TargetControlID="DropDownList3"></cc1:CascadingDropDown>
<br/>
<br/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


Hi,

I have changed the web config maxJsonLength to 1000. But i dont have that many values. First dropdown is giving me a value but on the second dropdown I am getting method error 500. My webservice code is as follows.

Imports

System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml

<WebService(Namespace:=

"http://tempuri.org/")> _
<System.Web.Script.Services.ScriptService()> _

Public

Class MIService
Inherits System.Web.Services.WebService
Shared _DocumentAs XmlDocument
Shared _lockAsNewObject
PublicReadOnlyProperty Document()As XmlDocument
Get
If (_DocumentIsNothing)Then
SyncLock _lock
_Document =New XmlDocument
_Document.Load(HttpContext.Current.Server.MapPath("~/App_Data/MIService.xml"))
EndSyncLock
EndIf
Document = _Document
ExitProperty
EndGet
EndProperty
PublicReadOnlyProperty Hierarchy()AsString()
Get
Dim _HeirarchyAsString() = {"city","zip"}
Return _Heirarchy
EndGet
EndProperty
<WebMethod()> _
PublicFunction GetDropDownContents(ByVal knownCategoryValuesAsString,ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()
'Get a dictionary of known category/value pairs
Dim knownCategoryValuesDictionaryAsNew StringDictionary
knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)' Perform a simple query against the data document
Return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category)
EndFunction
EndClass

My code for aspx file is:

<%

@.PageLanguage="VB"AutoEventWireup="true"CodeFile="Default.aspx.vb"Inherits="_Default"EnableEventValidation="False"%>
<%@.RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="cc1" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
<scriptrunat="server">
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
PublicSharedFunction GetDropDownContentsPageMethod(ByVal knownCategoryValuesAsString,ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()
ReturnNew MIService().GetDropDownContents(knownCategoryValues, category)EndFunction
</script>
</head>
<body>
<formid="form1"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<div>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:DropDownListID="DropDownList1"runat="server"Width="200px">
</asp:DropDownList><br/>
<asp:DropDownListID="DropDownList2"runat="server"Width="200px">
</asp:DropDownList><br/>
<asp:DropDownListID="DropDownList3"runat="server"AutoPostBack="True"Width="200px">
</asp:DropDownList><br/>
<br/>
<asp:LabelID="Label1"runat="server"Width="224px"></asp:Label><br/>
<br/>
<cc1:CascadingDropDownID="CascadingDropDown1"runat="server"Category="city"LoadingText="[loading City...]"PromptText="Please select a city."ServiceMethod="GetDropDownContents"ServicePath="MIService.asmx"TargetControlID="DropDownList1"></cc1:CascadingDropDown><cc1:CascadingDropDownID="CascadingDropDown2"runat="server"Category="zip"LoadingText="[Loading Zip...]"ParentControlID="DropDownList1"PromptText="Please enter zip."ServiceMethod="GetDropDownContentPageMethod"TargetControlID="DropDownList2"></cc1:CascadingDropDown><cc1:CascadingDropDownID="CascadingDropDown3"runat="server"Category="county"LoadingText="[loading County..]"ParentControlID="DropDownList2"PromptText="Please select county."ServiceMethod="GetDropDownContents"ServicePath="MIService.asmx"TargetControlID="DropDownList3"></cc1:CascadingDropDown>
<br/>
<br/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Thank You,

Nepalaya


Hi,

what's the content of the 500? You can useFiddler to examine it.


I hope u get it the way you want, but 765 items in a drop down list isn't very user friendly? Think about screen res?

Maybe test out autocomplete control?

Just giving my view, but if you want 1000 items in a drop down list, go for it :P (I know how annoying it is when people try to divert you from the solution you want, so go for what is best in your app, just throwing out a suggestion)


Hi,

The problem resolved. It was not related with the lenght. Page method was not working so change the service method name to GetDropDownContents and it works.

<

cc1:CascadingDropDownID="CascadingDropDown1"runat="server"Category="city"LoadingText="[loading City...]"PromptText="Please select a city."ServiceMethod="GetDropDownContents"ServicePath="MIService.asmx"TargetControlID="DropDownList1">
</cc1:CascadingDropDown><cc1:CascadingDropDownID="CascadingDropDown2"runat="server"Category="zip"LoadingText="[Loading Zip...]"ParentControlID="DropDownList1"PromptText="Please enter zip."ServicePath="MIService.asmx"ServiceMethod="GetDropDownContents"TargetControlID="DropDownList2">
</cc1:CascadingDropDown>

Thanks,

Nepalaya


Hello freinds,

I am not able to fill the cascading drop down using database. Does any one have fill it with the database??

Please help me i am stuck with this problem.

Thaks in advance.


Hi,

I get method error 500 in dropdownlist1 . Iam using WEB SERVICE .

i used <jsonSerializationmaxJsonLength="500" /> in web.config but still i get the error.

Can any one help ?

Thanks

CascadingDropDownList - Am i being a complete tool?

Hey,

Trying to get my head round these CascadingDropDownLists but i am continually returned with a [Method Error 500] error.

This is my code for 'Default.aspx'


<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"
Category="Author"
TargetControlID="DropDownList1"
ServiceMethod="FillMe"
ServicePath="WebService.asmx"
PromptText="Please select a category"
>
</ajaxToolkit:CascadingDropDown>
</form>
</body>
</html>

And my webservice, WebService.asmx, is as follows:

using System;using System.Configuration;using System.Web;using System.Collections;using System.Collections.Generic;using System.Collections.Specialized;using System.Web.Services;using System.Web.Services.Protocols;using AjaxControlToolkit;using System.Data;using System.Data.SqlClient;/// <summary>/// Summary description for WebService/// </summary>[WebService(Namespace ="http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WebService : System.Web.Services.WebService { [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public static CascadingDropDownNameValue[] FillMe(string knownCategoryValues, string category) { string connString = ConfigurationManager.ConnectionStrings["tenderlistDB"].ConnectionString; SqlConnection conn = new SqlConnection(connString); DataTable dt = new DataTable(); string Sql = "SELECT [ID], [Name] FROM tblCategories WHERE ParentID=@dotnet.itags.org.ParentID ORDER BY [Name] ASC"; try { conn.Open(); SqlCommand comm = new SqlCommand(Sql, conn); comm.Parameters.AddWithValue("@dotnet.itags.org.ParentID", "0"); SqlDataAdapter adapt = new SqlDataAdapter(comm); adapt.Fill(dt); } finally { conn.Close(); } List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); for (int i = 0; i < dt.Rows.Count; i++) { CascadingDropDownNameValue CCD = new CascadingDropDownNameValue(); CCD.name = dt.Rows[i]["Name"].ToString(); CCD.value = dt.Rows[i]["ID"].ToString(); values.Add(CCD); }return values.ToArray(); }}

Anyone have an idea as to why it's just not working?

Cheers,

Sean


Most likely you have an exception being thrown in your WebMethod. Set a breakpoint in it and use the site in Debug mode. Step through the WebMethod and see if you are getting an exception. This is usually the case for me. The other thing I have seen is too much information being returned, but I think that is another error number. The Execption will not be returned by the Web Service, so AJAX throws a 500 error up at you.


Thanks for getting back to me.

I've never really used the debug function, but i stuck some breakpoints in the code and hit F5, and it all ran ok.

If i run it in debug mode, instead of Error method 500, i get an Error Method 12031...Could that be anything?

In terms of the SQL, it returns 12 rows when run through Sql Query Analyzer.

Appreciate your help!


Been thinking about this a little over lunch. Have you marked your Web Service and Web methods with AJAX attributes?

The Class should have this attribute:

<System.Web.Script.Services.ScriptService()> _

The Web Method this one:

<System.Web.Script.Services.ScriptMethod()> _

So a method would resemble the following:

<WebMethod()> _

<System.Web.Script.Services.ScriptMethod()> _

PublicFunction GetMarkets(ByVal knownCategoryValuesAsString, _

ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()

Dim kvAs StringDictionary = _

AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

Dim RegionIdAsInteger

IfNot kv.ContainsKey("Region")OrElseNot Int32.TryParse(kv("Region"), RegionId)Then

ReturnNothing

EndIf

Dim lcAsNew LocMarketController

Dim itemListAs LocMarketList = lc.GetLocMarketByRegion(RegionId)

Dim valueAsNew List(Of AjaxControlToolkit.CascadingDropDownNameValue)

ForEach itemAs LocMarketInfoIn itemList

value.Add(New AjaxControlToolkit.CascadingDropDownNameValue(item.Market, item.MarketId))

Next

Return value.ToArray

EndFunction


Hey,

Tried that but still no joy. Starting to get frustrating now hehe, anyone else got any ideas?

Cheers for the help!


Hey,


Just to let you all know i got it working...i rewrote it this morning when i got into work and its all working fine...not sure why though, i have a feeling I wasn't passing through the correct parameters, so when the


'StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);'

line was hit, the method would break!

Thanks for all the help.