开发者

Constant Error 500 Cascading DropDown Lists in AjaxToolkit 4.0 ASP.NET

开发者 https://www.devze.com 2023-03-26 09:52 出处:网络
Im posting the issue down here cause this thing just wrecks my head for the last few weeks actually. I left it for a while but I would really use that tool if I can.

Im posting the issue down here cause this thing just wrecks my head for the last few weeks actually. I left it for a while but I would really use that tool if I can.

I get the generic Error 500 all the time in the dropdown.

Here's the code for the service:

using System;
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 inputHoursWS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, 开发者_高级运维using ASP.NET AJAX, 
//uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class inputHoursWS : System.Web.Services.WebService {

    public inputHoursWS () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public CascadingDropDownNameValue[] getGroupCodes(string knownCategoryValues, 
    string Category)
    {
        InputHoursDSTableAdapters.TMS_ProjectGroupsTableAdapter projectGroupTA = new InputHoursDSTableAdapters.TMS_ProjectGroupsTableAdapter();
        InputHoursDS.TMS_ProjectGroupsDataTable groups = projectGroupTA.GetProjectGroups();

        List<CascadingDropDownNameValue> groupValues = new List<CascadingDropDownNameValue>();

        foreach (DataRow row in groups)
        {

            int groupId = (int)row["ProjectGroupID"];
            string groupCode = (string)row["ProjectGroup"];

            groupValues.Add(new CascadingDropDownNameValue(groupCode, groupId.ToString()));

        }

        return groupValues.ToArray();
    }

 }

Here's the code for the front end web form:

  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

                    <asp:DropDownList ID="ddlGroups" runat="server" Width="70px" Height="21px">
                     </asp:DropDownList>
                     <asp:CascadingDropDown ID="ddlGroups_CascadingDropDown" 
                      runat="server" 
                      TargetControlID="ddlGroups" 
                      Category="ProjectGroup" 
                      PromptText="Choose a Group" 
                      LoadingText="Please wait ..." 
                      ServicePath="inputHoursWS.asmx" 
                      ServiceMethod="getGroupCodes">
                      </asp:CascadingDropDown>

The methods in the InputHoursDS Dataset are working correctly. I am accessing the developing machine remotely. There's a Visual Web Developer 2010 installed and also the IIS 6 is running .NET 4.0

And a quick question also related - What value do you put in the "Category" attribute on the front end? Is it anything from the database headings or its your own heading?


For me adding the below configuration to my web.config file resolved the issue:

<configuration>

  <!-- ... -->

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647"/>
      </webServices>
    </scripting>
  </system.web.extensions>

  <!-- ... -->

</configuration>

I'd been seeing the issue in a dropdown which lists the cities available for a given country. Most countries we only had a few cities defined for, but for one there were thousands; it was only that country which errorred; though when debugging the code looked fine (since the called method was running and returning results - the results were just larger than the acceptable size. Amending the max JSON length removed this artificial issue.


OK guys, I guess what I didn't realize is that the dataset wasn't set up correctly and actually in another approach I confused the method names.

Use debugging tools such as Fiddler. It really helped me.

To answer my own question. How I understand it is that the category is the primary key column for the next dropdown and the knownCategoryValues are the values of the previous key. Here's a piece of it actually working so that all of you looking for an answer can use it:

[WebMethod]
   public CascadingDropDownNameValue[] getGroups(string knownCategoryValues, string category)
  {
    StringDictionary categoryValues =
 AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(
 knownCategoryValues);

    int countryID = Convert.ToInt32(categoryValues["ProjectGroupID"]);

    List<CascadingDropDownNameValue> cascadingValues =
        new List<CascadingDropDownNameValue>();

    inputHrsDataSetTableAdapters.TMS_ProjectGroupsTableAdapter projectGroupsTA = new inputHrsDataSetTableAdapters.TMS_ProjectGroupsTableAdapter();


    foreach (DataRow _row in projectGroupsTA.GetProjectGroupCodes())
    {
        cascadingValues.Add(new CascadingDropDownNameValue(_row["ProjectGroup"].ToString(),
            _row["ProjectGroupID"].ToString()));

    }

    return cascadingValues.ToArray();
 }

  [WebMethod]
   public CascadingDropDownNameValue[] getProjectNumbers(string knownCategoryValues, string category)
{
    StringDictionary categoryValues =
 AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(
 knownCategoryValues);

    int GroupaID = Convert.ToInt32(categoryValues["ProjectGroupID"]);

    List<CascadingDropDownNameValue> cascadingValues =
        new List<CascadingDropDownNameValue>();

    inputHrsDataSetTableAdapters.TMS_ProjectRegisterTableAdapter projectRegisterTA = new inputHrsDataSetTableAdapters.TMS_ProjectRegisterTableAdapter();


    foreach (DataRow _row in projectRegisterTA.GetProjectNumbersFromGroupID(GroupaID))
    {
        cascadingValues.Add(new CascadingDropDownNameValue(_row["ProjectNumber"].ToString(),
            _row["ProjectNumber"].ToString()));

    }

    return cascadingValues.ToArray();

 }

And Also:

 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

                                   <asp:DropDownList ID="ddlGroupCodes" runat="server" Height="21px" Width="70px" />

                                   <asp:CascadingDropDown ID="ddlGroupCodes_CascadingDropDown" runat="server" 
                                       TargetControlID="ddlGroupCodes" 
                                       Category="ProjectGroupID"
                                        ServicePath="~/GroupService.asmx"
                                        ServiceMethod="getGroups" 
                                        PromptText="Select Group"
                                         LoadingText="Loading...">
                                   </asp:CascadingDropDown>

                                    <asp:DropDownList ID="ddlProjectNumbers" runat="server" Height="21px" Width="85px" />

                                   <asp:CascadingDropDown ID="ddlProjectNumbers_CascadingDropDown" runat="server" 
                                       TargetControlID="ddlProjectNumbers" 
                                       Category="ProjectNumber"
                                        ServicePath="~/GroupService.asmx"
                                        ServiceMethod="getProjectNumbers" 
                                        PromptText="Select Project No."
                                         LoadingText="Loading..." ParentControlID="ddlGroupCodes">
                                   </asp:CascadingDropDown>

Got all of it nicely thanks to Fiddler! Really, use it for debugging, its awesome.

Again use Debugging tools!!! You don't want to spend a lifetime on a simple misspelling of a method :).

0

精彩评论

暂无评论...
验证码 换一张
取 消