1

我有 2 个下拉菜单,第一个下拉菜单工作正常,显示数据库中的数据,但第二个下拉菜单显示错误 500。这是我的 web 服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Data;


[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 CascadingDropdown : System.Web.Services.WebService
{
private static  string strconnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//private static string strconnection =  ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection concategory = new SqlConnection(strconnection);

public CascadingDropdown()
{

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

[WebMethod]
public CascadingDropDownNameValue[] BindCategoryDetails(string knownCategoryValues, string category)
{

    concategory.Open();
    SqlCommand cmdcategory = new SqlCommand("select * from Categories", concategory);
    //create list and add items in it by looping through dataset table
    List<CascadingDropDownNameValue> categorydetails = new List<CascadingDropDownNameValue>();
    SqlDataReader drcategory= null ;
    drcategory=cmdcategory.ExecuteReader();
    while(drcategory.Read())
    {
         string    CategoryID = drcategory ["categoryID"].ToString();
         string    CategoryName = drcategory ["categoryName"].ToString();
         categorydetails.Add(new CascadingDropDownNameValue(CategoryName, CategoryID));
    }
    concategory.Close();
    return categorydetails.ToArray();
}
/// <summary>
/// WebMethod to Populate State Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindProductDetails(string knownCategoryValues, string category)
{
    int categoryID;
    //This method will return a StringDictionary containing the name/value pairs of the currently selected values
    StringDictionary categorydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    categoryID = Convert.ToInt32(categorydetails["Category"]);
    concategory.Open();
    SqlCommand cmdproduct = new SqlCommand("select * from Products where categoryID=@categoryID", concategory);
    cmdproduct.Parameters.AddWithValue("@categoryID", categoryID);        
    //create list and add items in it by looping through dataset table
    List<CascadingDropDownNameValue> productdetails = new List<CascadingDropDownNameValue>();      
     SqlDataReader drproduct= null ;
     drproduct=cmdproduct.ExecuteReader();       
    while(drproduct.Read())        {
         string    ProductID = drproduct ["categoryID"].ToString();
         string   ProductName = drproduct ["categoryName"].ToString();
         productdetails.Add(new CascadingDropDownNameValue(ProductName, ProductID));
    }
    concategory.Close();      
    return productdetails.ToArray();

}

}

和aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  EnableEventValidation="false"%>
<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" tagPrefix="ajax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:DropDownList ID="ddlcategory" runat="server"></asp:DropDownList>
<ajax:CascadingDropDown ID="ccdCategory" runat="server" Category="category" 
    TargetControlID="ddlcategory" PromptText="Select Category" 
    LoadingText="Loading Categories" ServiceMethod="BindCategoryDetails" 
    ServicePath="CascadingDropdown.asmx">
</ajax:CascadingDropDown>
<asp:DropDownList ID="ddlproduct" runat="server">
</asp:DropDownList>
<ajax:CascadingDropDown ID="ccdProduct" runat="server" Category="product"
 TargetControlID="ddlproduct" PromptText="Select Product"
  LoadingText="Loading Products" ServiceMethod="BindProductDetails"
   ScriptPath="CascadingDropdown.asmx">
   </ajax:CascadingDropDown>
</form>
</body>
</html>

如果还有什么你需要知道的告诉我。谢谢。

4

1 回答 1

0

我认为错误发生在您将 CategoryID 分配给 ProductID 的情况下。在第二个下拉列表绑定代码中

 string    ProductID = drproduct ["categoryID"].ToString();
 string   ProductName = drproduct ["categoryName"].ToString();

上面的代码应该换成这个

string    ProductID = drproduct ["ProductID"].ToString();
string   ProductName = drproduct ["ProductName"].ToString();
于 2012-07-13T15:03:21.387 回答