Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Webservices and Ajax CascadingDropDown and ScriptService

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I'm trying to get an Ajax CascadingDropDown Control working which gets the values via a webservice from a MS SQL database. I'm based the code on that in

I'm using Ajax toolkit v1.0.10301.0 (Have too as I'm working on part of someone else site)

When I run the page with the CascadingDropDown Control the drop down comes up but with just the prompt text and a 'Method Error 500' in each drop down lists.

Iv'e checked the web and its given advice on settings in the web.config which Iv'e done, but the main advice seems to be to add:
<Microsoft.Web.Script.Services.ScriptService()> _
just before the class or the service

ie
<Microsoft.Web.Script.Services.ScriptService()> _
Public Class CarsService

......

End Class

or just before the functions
<Microsoft.Web.Script.Services.ScriptService()> _
Public Shared ....

in and a number of other links.

When I try this though I get visual studio saying
<Microsoft.Web.Script.Services.ScriptService()> _
is not defined change to
<Script.Services.ScriptService()>
(which is then says is not valid for this declaration type)
or
<System.Web.Script.Services.ScriptMethodAttribute()> _
(which it accepts but doesn't seem to help)

I also tried
<System.Web.Script.Services()> _

which also gave the same suggestions

I tried Iv'e tried
Imports System.Web.Script.Services
and whilst it accepts the import it still give the VS warning

I check that System.Web.Extensions.dll is in my references and it is.

I'm using Visual Studio 2008 with asp.net 2 and ajax Toolkt 1. Can anyone advise me why I can't put the ScriptService() that others sugest

Thanks
Andy
 
Finally got it to accept <System.Web.Script.Services.ScriptService()> _

which I guess is the correct one, but it doesn't seem to help I'm still getting the 'Method Error 500'. Does anyone have any suggestions, the web service code Iv'e written is below


Imports System
Imports System.Web
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports AjaxControlToolkit
Imports System.Data
Imports System.Data.SqlClient

''' <summary>
''' Summary description for Productdata
''' </summary>
<WebService([Namespace]:=" _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Script.Services.ScriptService()> _
Public Class CatItemsService
Inherits System.Web.Services.WebService
Public Sub New()
'Uncomment the following line if using designed components
'InitializeComponent();
End Sub

<WebMethod()> _
Public Shared Function GetMainItems(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Catalogue").ToString)
Dim cmd As New SqlCommand("SELECT ID, txtDescription FROM MainItems", con)
Dim itemsDataSet As New DataSet
cmd.CommandType = CommandType.Text
con.Open()
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter(cmd)
' Fill the DataSet
myDataAdapter.Fill(itemsDataSet)
' Close the connection
con.Close()
Dim values As New List(Of CascadingDropDownNameValue)()
For Each dr As DataRow In itemsDataSet.Tables(0).DefaultView
Dim mainItemDescription As String = DirectCast(dr("txtDescription"), String)
Dim mainItemSerial As Integer = CInt(dr("ID"))
values.Add(New CascadingDropDownNameValue(mainItemDescription, mainItemSerial.ToString()))
Next
Return values.ToArray()
End Function

<WebMethod()> _
Public Shared Function GetOptionsForProduct(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
Dim ProductId As Integer
If Not kv.ContainsKey("Product") OrElse Not Int32.TryParse(kv("Product"), ProductId) Then
Return Nothing
End If
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Catalogue").ToString)
Dim cmd As New SqlCommand("usp_relatedoptions_list", con)
Dim itemsDataSet As New DataSet
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@itemID", SqlDbType.Int, 4))
cmd.Parameters("@itemID").Value = ProductId
con.Open()
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter(cmd)
' Fill the DataSet
myDataAdapter.Fill(itemsDataSet)
' Close the connection
con.Close()

Dim values As New List(Of CascadingDropDownNameValue)()
For Each dr As DataRow In itemsDataSet.Tables(0).DefaultView
values.Add(New CascadingDropDownNameValue(DirectCast(dr("txtDescription"), String), dr("ID").ToString()))
Next
Return values.ToArray()
End Function

End Class

and this is the CascadingDropDownCode that uses it

<ajaxToolkit:CascadingDropDown
ID="CascadingDropDown1"
runat="server"
TargetControlID="ddlProducts"
Category="Product"
PromptText="Select a main item"
ServicePath="SercoCatItemsService.asmx"
ServiceMethod="GetMainItems" />
<ajaxToolkit:CascadingDropDown
ID="CascadingDropDown2"
runat="server"
TargetControlID="ddlOptions"
ParentControlID="ddlProducts"
PromptText="Select an option"
ServiceMethod="GetOptionsForProduct"
ServicePath="SercoCatItemsService.asmx"
Category="Option" />
 
if my memory serves me correctly you need to disable viewstate validation or something like that. because the html is altered, but the viewstate is not and webforms validates viewstate by default.

it would be done in the page declaration
Code:
<%@page codebehind="..." inherits="..." enableviewstatevalidation="false" %>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks jmeckley,
Tried this but it didn't seem to help unfortunately. Also set ValidateRequest="false" which I gather your'e supposed to do.

Thanks for the suggestion though. Must admit I'm completely flumuxed by this and have no idea how to progress
Thanks
AndyH1
 
I think the problem lies with my web service rather than the cascadingDropDown.

I put a test routine in my webservice:

<WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Function Test(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim values As New List(Of CascadingDropDownNameValue)()
values.Add(New CascadingDropDownNameValue("Test1", "11"))
values.Add(New CascadingDropDownNameValue("Test2 - Latitude D430", "5"))
Return values.ToArray()
End Function

and this runs ok when I invoke it giving
<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfCascadingDropDownNameValue xmlns:xsi=" xmlns:xsd=" xmlns="- <CascadingDropDownNameValue>
<name>Test1</name>
<value>11</value>
<isDefaultValue>false</isDefaultValue>
</CascadingDropDownNameValue>
- <CascadingDropDownNameValue>
<name>Test2 - Latitude D430</name>
<value>5</value>
<isDefaultValue>false</isDefaultValue>
</CascadingDropDownNameValue>
</ArrayOfCascadingDropDownNameValue>

But if I run GetMainItems I get cannot display page, and if I run GetOptionsForProduct I get

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfCascadingDropDownNameValue xmlns:xsi=" xmlns:xsd=" xsi:nil="true" xmlns=" />

presumably because its activating the
If Not kv.ContainsKey("Product") OrElse Not Int32.TryParse(kv("Product"), ProductId) Then
Return Nothing
End If
branch (which it should as I'm not putting in a key value)

If I put
values.Add(New CascadingDropDownNameValue("TRACE", "1111"))

in the GetMainItems routine, ie
For Each dr As DataRow In itemsDataSet.Tables(0).DefaultView
values.Add(New CascadingDropDownNameValue(DirectCast(dr("txtDescription"), String), CInt(dr("ID")).ToString()))
Next
values.Add(New CascadingDropDownNameValue("TRACE", "1111"))
Return values.ToArray()

it appears in the drop down BUT only if I comment out the
For Each dr As DataRow In itemsDataSet.Tables(0).DefaultView
...
Next
lines so it looks as if
For Each dr As DataRow In itemsDataSet.Tables(0).DefaultView
cause the error. I can't see anything wrong with this though
 
Have got the GetMainItems running ok in that when I run the service and invoke it its now giving the correct results, however when I try to use it in the CascadingDropDown I'm still getting the 'Method Error 500' in the dropdown list box
AndyH1
 
Got it, just needed
<Script.Services.ScriptService()> _
on the webservices class back
AndyH1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top