Wednesday, March 21, 2012

Question: Using CascadingDropDown with OleDb

I'm curious. Do youhave to use a Web Service when trying to pull items for a cascading drop down from a database or is there another (perhaps simpler) way? I'd love to be more enlightened on the subject. Thanks in advance.

Hi Venom,

VenomZ302:

Using CascadingDropDown with OleDb

I have made a sample. Hope it helps.

ASPX:

<%@. Page Language="C#" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!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 id="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> State:<asp:DropDownList runat="server" ID="dlState"> </asp:DropDownList> City: <asp:DropDownList runat="server" ID="dlCity"> </asp:DropDownList> <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="dlState" Category="State" PromptText="Select a state" ServicePath="../WebService/CityServiceOledb.asmx" ServiceMethod="GetStates" LoadingText="Load Text..."> </cc1:CascadingDropDown> <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="dlCity" ParentControlID="dlState" Category="City" PromptText="Select a city" ServicePath="../WebService/CityServiceOledb.asmx" ServiceMethod="GetCities" LoadingText="Load Text..."> </cc1:CascadingDropDown> </form></body></html>

WebService:

<%@. WebService Language="C#" Class="CityServiceOledb" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Specialized;
using System.Collections.Generic;
using AjaxControlToolkit;
using System.Data.OleDb;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class CityServiceOledb : System.Web.Services.WebService {

protected OleDbConnection cn;
protected OleDbCommand cm;
protected OleDbDataReader myReader;
private string strSql = "";
//private string conStr = "data source=TASKONE\\SQLEXPRESS;initial catalog=TestDB;user id=sa;pwd=password;Pooling=True; Min Pool Size=0; Max Pool Size=1000; Connection Lifetime=30;";

//private string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWork\\DB\\YourDBName.mdb";
//using Microsoft Office2007
private string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyWork\\DB\\Database1.accdb";
public CityServiceOledb() { }

[WebMethod]
public CascadingDropDownNameValue[] GetStates(string knownCategoryValues, string category)
{
strSql = "SELECT * FROM tb_State";
GetDr(strSql);
List<CascadingDropDownNameValue> myList = new List<CascadingDropDownNameValue>();
while (myReader.Read()) {
myList.Add(new CascadingDropDownNameValue(myReader["StateName"].ToString(), myReader["StateID"].ToString()));
}
myReader.Close();
return myList.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int stateID;
if (!kv.ContainsKey("State") || !Int32.TryParse(kv["State"], out stateID))
{
return null;
}
List<CascadingDropDownNameValue> myList = new List<CascadingDropDownNameValue>();
strSql = "SELECT * FROM tb_City WHERE StateID=" + stateID.ToString();

GetDr(strSql);
while (myReader.Read())
{
myList.Add(new CascadingDropDownNameValue(myReader["CityName"].ToString(), myReader["CityID"].ToString()));
}
myReader.Close();
return myList.ToArray();
}

#region DB Operate
public void Open()
{
if (cn == null)
{
cn = new OleDbConnection(conStr);
cn.Open();
}
}
public void Close() {
if (cn != null)
{
cn.Close();
cn = null;
}
}

public OleDbDataReader GetDr(string strSql) {
Open();
cm = new OleDbCommand(strSql,cn);
myReader = cm.ExecuteReader();
//myReader = cm.ExecuteReader();
return myReader;
}
#endregion DBOperate

}

Note: You should add two tables with the name "tb_State" and "tb_City" into your Database ,which is used in your project , before you using this demo.

Table "tb_State" has two fields with the name "StateID"(primarykey) and "StateName"(Text).

Table "tb_City" has three fields with the name "CityID"(primarykey)、"StateID"(Number) and "CityName"(Text).

VenomZ302:

I'm curious. Do youhave to use a Web Service when trying to pull items for a cascading drop down from a database or is there another (perhaps simpler) way? I'd love to be more enlightened on the subject. Thanks in advance.

We are not limited to use Web Service. Sometimes, we may add the implement code into our *.cs file directly. Here is the sample:

using System;using System.Web.Services;using AjaxControlToolkit;public partialclass CascadingDropDown_CascadingDropDown : CommonPage{protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e) {// Get selected values } [WebMethod] [System.Web.Script.Services.ScriptMethod]public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues,string category) {
 //obtain data from Database.
 return;
 }}
Note: We needn't to set the ServicePath property for the control in this case. Just like below.
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
Details please dip into the Ajax ControlToolkit source code.

If I misunderstood you, please let me know.


Thanks for the information. I really appreciate it. Now, however, I've progressed to the point where I need to understand more of the error codes that are given. Most specifically, error 12030, as found in my thread here: http://forums.asp.net/t/1138732.aspx

Thanks again.


Hi Venom,

Error code: 12030

ERROR_INTERNET_CONNECTION_ABORTED
The connection with the server has been terminated.

In your case, it seems like your webservice doesn't work properly.I suggest that you should add break points into your webservice and debug step-by-step.

Note: If you made some modifications in your webservice and before you debug , please reset your aplication by saving your web.config file.Good luck! Big Smile

No comments:

Post a Comment