Answered by:
Using CascadingDropDownList with contextKey generates 500 error when contextKey is null or missing

Question
-
User-166193360 posted
So here's the situation: I have a category/subcategory series of dropdown lists, which will assign a category/categories to a company. Since the company information is stored in a database (as are the cats/subcats), I wanted to select default categories/subcategories based on the data retrieved (e.g. if a company was part of Cat_ID 1 and subcat_ID 22, I'd want to select Cat_ID 1 from a category dropdown and Subcat_ID 22 from a subcategory dropdown.
The only way I could figure out to select Subcat_ID 22 from the Subcat dropdown is to use contextKey, which does work. However, the code I used generates the following error whenever a contextKey doesn't exist:
{"Message":"Invalid web service call, missing value for parameter: \u0027contextKey\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
Here's the code I used to generate the subcategory CascadingDropDown:
Public Function GetSubcategories(ByVal knownCategoryValues As String, ByVal category As String, ByVal contextKey As String) As CascadingDropDownNameValue() If String.IsNullOrEmpty(contextKey) Then contextKey = "0" End If Dim Connect As OleDbConnection = New OleDbConnection(Vars.DS) Connect.Open() Dim SQL_QUery As String = "Select Cat_ID, Category from Categories where Parent_Category = @Cat_ID and Parent_Category <> 0 and (Sample_Cat = False or Sample_Cat = null) order by Cat_Order" Dim Cat_Values As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues) Dim Cat_ID As Integer If Not Cat_Values.ContainsKey("Category") Or Not Int32.TryParse(Cat_Values("Category"), Cat_ID) Then Return Nothing End If Dim RS As OleDbCommand = New OleDbCommand(SQL_QUery, Connect) RS.Parameters.AddWithValue("@Cat_ID", Cat_ID) Dim Data_Reader As OleDbDataReader = RS.ExecuteReader() Dim Subcategories As New List(Of CascadingDropDownNameValue) While Data_Reader.Read() Dim Subcat As String = Data_Reader("Category") Dim Subcat_ID As String = Data_Reader("Cat_ID") Dim New_Item As CascadingDropDownNameValue If Subcat_ID <> contextKey Then New_Item = New CascadingDropDownNameValue(Subcat, Subcat_ID, False) Else New_Item = New CascadingDropDownNameValue(Subcat, Subcat_ID, True) End If Subcategories.Add(New_Item) End While Data_Reader.Dispose() RS.Dispose() Connect.Dispose() Return Subcategories.ToArray End Function
As you can see, I attempted to assign a contextKey value when one didn't exist. It didn't matter. Everything else in this code did work before I attempted using contextKey.
The question I have is: is there another way to assign a default value without using contextKey (keeping in mind that I tried to use the SelectedValue property for each child dropdownlist on Page_Load and Page_LoadComplete and neither worked)?
Thanks.
Monday, September 14, 2009 11:09 PM
Answers
-
User-738352979 posted
<?xml:namespace prefix = cc1 /><cc1:CascadingDropDown id=CascadingDropDown1 runat="server" ContextKey="" UseContextKey="true"></cc1:CascadingDropDown>make sure in ur aspx u have used UseContextKey="true" ContextKey="" these two attribute
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 15, 2009 12:53 AM
All replies
-
User-738352979 posted
<?xml:namespace prefix = cc1 /><cc1:CascadingDropDown id=CascadingDropDown1 runat="server" ContextKey="" UseContextKey="true"></cc1:CascadingDropDown>make sure in ur aspx u have used UseContextKey="true" ContextKey="" these two attribute
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 15, 2009 12:53 AM -
User1839833660 posted
Hi
You need to create webservice and webmethod to populate dropdowns
Refer link below for more info
Tuesday, September 15, 2009 12:59 AM -
User-166193360 posted
make sure in ur aspx u have used UseContextKey="true" ContextKey="" these two attribute
Thanks, dude. This actually was all that was wrong. Funny, though...I never saw these attributes in any of the documentation that I had read on the topic of CascadingDropDownLists (then again, I had to piece what I was doing together from about 8 different websites.)
Tuesday, September 15, 2009 1:07 AM