hello,
I would like to populate Cascading DropDownList From SQL Server.
I prepare my project the same way as in this article:
http://www.aspsnippets.com/Articles/AJAX-Cascading-DropDown-Example-in-ASPNet.aspx
but all the time I have "Method error 500" I read many articles how to solve this issue but without success.
Below there are my codes:
----SQL DB has two tables:
1.tblUAP (UAP (PK), Start_Date,End_Date)
2.tblGAP(idGAP (PK),GAP,UAP(FK to tblUAP))
---project1.aspx
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Project1.aspx.cs" Inherits="TEST.Project1" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
....
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
......
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:CascadingDropDown ID="DropDownList1_CascadingDropDown" runat="server"
Category ="UAP"
TargetControlID="DropDownList1"
PromptText ="-- Select UAP--"
LoadingText="[Searching UAP....] "
ServiceMethod="GetUAPs"
ServicePath ="Services.asmx">
</asp:CascadingDropDown>
...
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:CascadingDropDown ID="DropDownList2_CascadingDropDown" runat="server"
Category ="GAP"
TargetControlID="DropDownList2"
PromptText ="-- Select GAP--"
LoadingText="[Searching GAP....] "
ServiceMethod="GetGAPs"
ServicePath ="Services.asmx"
ParentControlID="DropDownList1">
</asp:CascadingDropDown>
----Services.asmx
using System;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;
using System.Collections.Generic;
namespace PDE
{
/// <summary>
/// Summary description for Services
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Services : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetUAPs(string knownCategoryValues)
{
string query = "SELECT UAP FROM tblUAP";
List<CascadingDropDownNameValue> UAPs = GetData(query);
return UAPs.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetGAPs(string knownCategoryValues)
{
string UAPs = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["UAP"];
string query = string.Format("SELECT GAP FROM tblGAP WHERE UAP = {0}", UAPs);
List<CascadingDropDownNameValue> GAPs = GetData(query);
return GAPs.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["Db_constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[1].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
}
}
Please, could you give me advice what else Can I check ??
Br.