Monday, March 26, 2012

CasdadingDropDown with Db in VB.net

Well I have tried to follow the example in c# and a great post using access. I am trying to populate the first dropdown with Activity categories from a SQL database called PA. I created a dataset with two difference datatables in it: One for populating the first with a distinct clause: Select Distring [Category] FROM [Compendium] SORT asc. The second ta with a Select Command of "Select [CaseCode], [Mets], [Description], [Category] Where [Category = @dotnet.itags.org.Category]

I set the CasdadingDropDown Properties as follows:

 <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server"> <cc1:CascadingDropDownProperties TargetControlID="ddActCat" ServiceMethod="GetActCat" ServicePath="ActType.asmx" PromptText="Choose an Activity Category" Category="Cat" /> <cc1:CascadingDropDownProperties TargetControlID="ddCatType" ServicePath="ActType.asmx" ServiceMethod="GetActType" PromptText="Choose Activity Type" ParentControlID="ddActCat" Category="Type" /> </cc1:CascadingDropDown>
 
The asmx file ActType.asmx:
 
<%@dotnet.itags.org. WebService Language="VB"Class="ActType" %>Imports System.WebImports System.Web.ServicesImports System.Web.Services.ProtocolsImports AtlasControlToolkitImports System.Collections.GenericImports System.DataImports System.Data.SqlClient<WebService(Namespace :="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _Public Class ActTypeInherits System.Web.Services.WebService <WebMethod()> _Public Function GetActCat(ByVal knownCategoryValuesAs String,ByVal categoryAs String)As AtlasControlToolkit.CascadingDropDownNameValue()Dim actAs New ActivityTableAdapters.ActivityCateogry_TblTableAdapter()Dim ActCatAs Activity.ActivityCateogry_TblDataTable = act.GetActCat()With ActCatDim intRowAs Integer Dim valuesAs List(Of CascadingDropDownNameValue) =New List(Of CascadingDropDownNameValue)For intRow = 0To .Rows.Count - 1 values.Add(New CascadingDropDownNameValue(.Rows(intRow).ItemArray(0).ToString, .Rows(intRow).ItemArray(0)))Next Return values.ToArrayEnd With Return New CascadingDropDownNameValue() {}End Function <WebMethod()> _Public Function GetActType(ByVal knownCategoryValuesAs String,ByVal categoryAs String)As CascadingDropDownNameValue()Dim kvAs StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)Dim CompCodeAs Integer If (kv.ContainsKey("Cat")And Int32.TryParse(kv("Cat"), CompCode))Then Dim taAs New ActivityTableAdapters.ActivityType_TblTableAdapter()Dim ActTypeAs Activity.ActivityType_TblDataTable = ta.GetType(CompCode)With ActTypeDim intRowAs Integer Dim valuesAs List(Of CascadingDropDownNameValue) =New List(Of CascadingDropDownNameValue)For intRow = 0To .Rows.Count - 1 values.Add(New CascadingDropDownNameValue(.Rows(intRow).ItemArray(3).ToString, .Rows(intRow).ItemArray(2)))Next Return values.ToArrayEnd With End If Return New CascadingDropDownNameValue() {}End Function End Class

The first dropDown name ddActCat populates fine, but the second does not. Also these dd's are in an updatePanel control and the Cdd Extender is outside the Updatepanel. I have tried it both ways. I need to have it inside the UpdatePanel becuase it is in line with other controls in a form. Should I seperate the others into two UpdatePanels and have the CDD outside?

I am really not sure what is going on in the second WebMethod GetType() please help.

I finally got the code to work after checking the WebService paths and making some minor adjustments to the table Items(Count). Here is my asmx file code that worked with SQL DataSet.

<%@. WebService Language="VB"Class="ActType" %>Imports System.WebImports System.Web.ServicesImports System.Web.Services.ProtocolsImports AtlasControlToolkitImports System.Collections.GenericImports System.DataImports System.Data.SqlClient<WebService(Namespace :="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _Public Class ActTypeInherits System.Web.Services.WebService <WebMethod()> _Public Function GetActCat(ByVal knownCategoryValuesAs String,ByVal categoryAs String)As AtlasControlToolkit.CascadingDropDownNameValue()Dim actAs New ActivityTableAdapters.ActivityCategory_TblTableAdapter()Dim ActCatAs Activity.ActivityCategory_TblDataTable = act.GetActCat()With ActCatDim intRowAs Integer Dim valuesAs List(Of CascadingDropDownNameValue) =New List(Of CascadingDropDownNameValue)For intRow = 0To .Rows.Count - 1 values.Add(New CascadingDropDownNameValue(.Rows(intRow).ItemArray(1).ToString, .Rows(intRow).ItemArray(0)))Next Return values.ToArrayEnd With Return New CascadingDropDownNameValue() {}End Function <WebMethod()> _Public Function GetActType(ByVal knownCategoryValuesAs String,ByVal categoryAs String)As CascadingDropDownNameValue()Dim kvAs StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)Dim CatIDAs Integer If (kv.ContainsKey("Category")And Int32.TryParse(kv("Category"), CatID))Then Dim taAs New ActivityTableAdapters.ActivityType_TblTableAdapter()Dim ActTypeAs Activity.ActivityType_TblDataTable = ta.GetType(CatID)With ActTypeDim intRowAs Integer Dim valuesAs List(Of CascadingDropDownNameValue) =New List(Of CascadingDropDownNameValue)For intRow = 0To .Rows.Count - 1 values.Add(New CascadingDropDownNameValue(.Rows(intRow).ItemArray(2).ToString, .Rows(intRow).ItemArray(1)))Next Return values.ToArray()End With End If Return New CascadingDropDownNameValue() {}End Function End Class

Most of the code came fromt converting the Example from the Toolkit and from post:

http://forums.asp.net/1362555/ShowThread.aspx#1362555

ThanksAthertonCreative is not online. Last active: 08-06-2006, 10:42 AMAthertonCreative you were very helpful with your post.

T


One problem I am having is that when I click on the second dd the wrong entry fills the dd text value. The Value of the dd is not the text value and is not unique, could it be looking to place the value in the dd instead of the text? By the By, I tried to do this with two dd's and a SQLDataSource with an Update panel. It worked, and had the same error. Help!!!


Maybe double-check that the order of the parameters to the CascadingDropDownNameValue constructor are correct? The correct order is: name, value.
Thanks for the reply dave. The dataset that gets returned has several values in the query. The first column is the ID, second is Mets, third is Description or Type of Activity, and fourth is CategoryID or (0,1,2,3). So I chose 2 for the Name and 1 for the Value. I want the Activity Type displayed as text and the Value to be the METS so I can calculate calories burnt more precisely. I checked and I have the pair set correctly. I also checked by not using a CDD but with two dd's from sqlDataSource and same problem. I have seen this issue before. When a large number of choice populate a DD then sometimes the wrong value appears when selected. I think it looks for the SelectedValue which is the same as the Item selected and choice the first one it comes to.
I think I can see how that might happen if you have multiple values that are the same. But I wouldn't think it would be a problem if you have multiple names that are the same. If you're seeing this without using CascadingDropDown at all, then it's probably pretty safe to say it's not responsible, but it might be interesting to get to the bottom of this anyway. :)

Well at least in this case I have to change my Cascading Drop downs anyway because I cannot get at the values and the needed selectedItem.Text value, which makes no sense to me what so ever. I would think since you have to use name-value pairs to retrieve the cdd's then you would want to be able to retrieve the SelectedItem.Text value somehow to write it back somewhere.

Write now I have to use the SelectedValue to get the ID of the item I want and make another DB call to get that value AGAIN, seems a waste.

Any help on either front would be great!


I was going to point you athttp://forums.asp.net/thread/1317472.aspx for some more information, but it looks like you're already part of that thread. :)

Dave: Thanks for your input, and I always search the posts prior to posting. I have learned many things that way.

One issue was that I had the DD's in post back mode from trying to use the sqldatasource without altas. after removing the property it works better now, but still has some hickups when alot of data populated the dd's. Not as many errors but still a few.

I also HATE the fact that we cannot get at the selectedItem.text value. This is a pain.


FYI, I've opened work item 1895 to track the .Text request.

No comments:

Post a Comment