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 LocMarketControllerDim 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.ToArrayEndFunction
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.
No comments:
Post a Comment