Monday, March 26, 2012

Confused By Cascading Dropdownlist

I am trying to use the AJAX cascading dropdownlist,

The original code is in C#, which Is not my language, here is the orignal code:

public CascadingDropDownNameValue[] GetProductsByCategoryID(string knownCategoryValues, string category)
{
string connectionString = SqlConnectionString.GetConnectionString();
string[] categoryValues = knownCategoryValues.Split(':', ';');
int categoryID = Convert.ToInt32(categoryValues[1]);

SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT ProductID, ProductName FROM Products WHERE CategoryID = @dotnet.itags.org.CategoryID", myConnection);
myCommand.Parameters.AddWithValue("@dotnet.itags.org.CategoryID", categoryID);

SqlDataAdapter ad = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
ad.Fill(ds);

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

foreach (DataRow row in ds.Tables[0].Rows)
{
cascadeCollection.Add(new CascadingDropDownNameValue((string)row["ProductName"], row["ProductID"].ToString()));

}
return cascadeCollection.ToArray();


}
}

I ran the code through a C# to VB translater this is what it returned.

Public CascadingDropDownNameValue() GetProductsByCategoryID(String knownCategoryValues, String category)
{
Dim connectionString As String = SqlConnectionString.GetConnectionString()
Dim categoryValues() As String = knownCategoryValues.Split(":"c,";"c)
Dim categoryID As Integer = Convert.ToInt32(categoryValues(1))

Dim myConnection As SqlConnection = New SqlConnection(connectionString)
Dim myCommand As SqlCommand = New SqlCommand("SELECT ProductID, ProductName FROM Products WHERE CategoryID = @dotnet.itags.org.CategoryID",myConnection)
myCommand.Parameters.AddWithValue("@dotnet.itags.org.CategoryID", categoryID)

Dim ad As SqlDataAdapter = New SqlDataAdapter(myCommand)
Dim ds As DataSet = New DataSet()
ad.Fill(ds)

Dim cascadeCollection As List<CascadingDropDownNameValue> = New List<CascadingDropDownNameValue>()

Dim row As DataRow
For Each row In ds.Tables(0).Rows
cascadeCollection.Add(New CascadingDropDownNameValue(CType(row("ProductName"),row("ProductID").ToString(), String)))

Next
Return cascadeCollection.ToArray()


}
}

Visual Studio complains about the first line, which doesn't loop right to me! I there anyone whod does both VB and C# who can point me in the right direction!

Or is there VB examples of the ASP.NET AJAX toolkit.

Thanks

I found the video for cascading Dropdoenlist, but it is retreving data from an file system xml file. I will have to use a database that will continue to grow so I need to alter the method.

What I am not sure how to handle is the properties that is defined; Is this need for records coming from a database?

here is the code that I am speaking about

1Public ReadOnly Property Document()As xmlDocument2Get3 If (_DocumentIs Nothing)Then4 SyncLock _lock5 _Document =New xmlDocument6 _Document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CarService.xml"))7End SyncLock89 End If1011 Document = _Document1213End Get14 End Property15 Public ReadOnly Property Hierarchy()As String()16Get17 Dim _HeirarchyAs String() = {"make","model"}18Return _Heirarchy19End Get20 End Property

If these Properties are needed, then is the Sync_lock, really necessary, or is the only because of the file based access menthod being demonstrated?

How would you load the datatable into the Property Document? Is it as simple as Document.Load (DatatableName)?

Is there an example of how to use the cascading dropdownlist from a database source, but in VB.Net somewhere on the Net?

All, the VP example, I downloaded from the video, has 13 build errors, when I tried to run the default.aspx page.

Help Please!


Here is an example, I have this wrapped in a seperate web service for reuse by multiple pages. This is the second drop down which has a parent with categoryBarId

<System.Web.Services.WebMethod(True)> _
Public Function GetFoo(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()

Dim values As New Collections.Generic.List(Of CascadingDropDownNameValue)
Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
Dim barId As Integer
If Not kv.ContainsKey("BarId") OrElse Not Int32.TryParse(kv("BarId"), barId) Then
Return Nothing
End If
Using myConnection As New System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FooConnectionString").ConnectionString)
Using myCommand As New System.Data.SqlClient.SqlCommand("Foo2StoredProcedure", myConnection)

myCommand.Parameters.Add("@.BarId", SqlDbType.Int).Value = barId
myCommand.CommandType = Data.CommandType.StoredProcedure
myConnection.Open()
Using myDataReader As System.Data.SqlClient.SqlDataReader = myCommand.ExecuteReader()
While myDataReader.Read()
values.Add(New CascadingDropDownNameValue(myDataReader.Item("FooDescription").ToString(), myDataReader.Item("FooId").ToString(), False))
End While

End Using
End Using
End Using
Return values.ToArray

End Function


Thank you,

I mostly follow what you are doing, I would mind finding good document about each one the Ajax controls and also a Level 100 or 200 document on creating web services.

If anyone knows of any please pass the url along.

No comments:

Post a Comment