在ajax与web服务下拉列表级联中,第二个下拉列表显示方法错误500

时间:2012-07-13 14:33:36

标签: c# asp.net cascadingdropdown

我有2个下拉列表,第一个下降工作正常并显示来自数据库的数据,但第二个下拉列表显示错误500.这是我的webservice代码:

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>

如果您需要知道其他任何事情,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

我认为您将CategoryID分配给ProductID时出现错误bcz。在第二个下拉列表绑定代码

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

上面的代码应该替换为

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