Conver c# code into

Posted on 2007-08-07
Last Modified: 2013-11-26
convert the following code into


    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 = @CategoryID", myConnection);
        myCommand.Parameters.AddWithValue("@CategoryID", categoryID);

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

        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();

Question by:devdept
    1 Comment
    LVL 7

    Accepted Solution

    <WebMethod()> _
    Public Function GetProductsByCategoryID(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
          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 New SqlConnection(connectionString)
          Dim myCommand As New SqlCommand("SELECT ProductID, ProductName FROM Products WHERE CategoryID = @CategoryID", myConnection)
          myCommand.Parameters.AddWithValue("@CategoryID", categoryID)

          Dim ad As New SqlDataAdapter(myCommand)
          Dim ds As New DataSet()

          Dim cascadeCollection As New List(Of CascadingDropDownNameValue)()

          For Each row As DataRow In ds.Tables(0).Rows

                cascadeCollection.Add(New CascadingDropDownNameValue(DirectCast(row("ProductName"), String), row("ProductID").ToString()))
          Return cascadeCollection.ToArray()

    End Function

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now