This is a migrated thread and some comments may be shown as answers.

Post full code for webservice used in databinding webservice example?

1 Answer 55 Views
Menu
This is a migrated thread and some comments may be shown as answers.
KevinMc
Top achievements
Rank 1
KevinMc asked on 16 Jan 2008, 09:03 PM
Can the full code for the webservice used in the online demo for binding prometheus menu to a webservice be posted. The stub does not help much for determining what we need to return from the webservice.

1 Answer, 1 is accepted

Sort by
0
Accepted
T. Tsonev
Telerik team
answered on 17 Jan 2008, 08:28 AM
Hello Kevin,

The full code is available in the App_Code folder of the QSF - ProductCategories.cs. Here is the menu-related code:

using System; 
using System.Web; 
using System.Collections; 
using System.Collections.Generic; 
using System.Collections.Specialized; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Threading; 
using System.Web.Script.Services; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using Telerik.Web.UI; 
 
 
[ScriptService] 
public class ProductCategories : WebService 
    [WebMethod] 
    public RadMenuItemData[] GetMenuCategories(RadMenuItemData item, object context) 
    { 
        IDictionary<stringobject> contextDictionary = (IDictionary<stringobject>) context; 
 
        DataTable productCategories = GetProductCategories(contextDictionary["CategoryID"]); 
 
        List<RadMenuItemData> result = new List<RadMenuItemData>(); 
 
        foreach (DataRow row in productCategories.Rows) 
        { 
            RadMenuItemData itemData = new RadMenuItemData(); 
            itemData.Text = row["Title"].ToString(); 
            itemData.Value = row["CategoryId"].ToString(); 
 
            if (Convert.ToInt32(row["ChildrenCount"]) > 0) 
            { 
                itemData.ExpandMode = MenuItemExpandMode.WebService; 
            } 
 
            result.Add(itemData); 
        } 
 
        return result.ToArray(); 
    } 
 
    private DataTable GetProductCategories(object categoryId) 
    { 
        SqlConnection connection = new SqlConnection( 
            ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString); 
 
        SqlCommand selectCommand = 
            new SqlCommand( 
@"
    SELECT
        pc1.CategoryID,
        pc1.Title,
        ISNULL(pc2.ChildrenCount, 0) as ChildrenCount
    FROM ProductCategories as pc1
    LEFT JOIN
        (
            SELECT   ParentId, COUNT(*) AS ChildrenCount
            FROM     ProductCategories
            Group By (ParentId)
        ) as pc2
    ON
        pc1.CategoryId = pc2.ParentId
    WHERE pc1.parentId = @parentId
"
        connection); 
 
        selectCommand.Parameters.AddWithValue("parentId", categoryId); 
 
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand); ; 
 
        DataTable productCategories = new DataTable(); 
        adapter.Fill(productCategories); 
 
        return productCategories; 
    } 
 
 


Greetings,
Tsvetomir Tsonev
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Menu
Asked by
KevinMc
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Share this question
or