Binding RadMenu to SQL Database

5 posts, 1 answers
  1. Flippie
    Flippie avatar
    8 posts
    Member since:
    Jan 2008

    Posted 22 Jun 2008 Link to this post

    "The ParentID of the root items must be null (nothing)" From a database perspective will it not be better to change the control to rather use "0" for the ParentID of the root items. According to my understanding one should try to avoide allowing null (nothing) in any data column. One possible solution might be to change the Dataset and replace all value in the ParentID for "0" to null (nothing) but I am not sure if this is possible?
  2. Answer
    Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 23 Jun 2008 Link to this post

    Hello Flippie,

    It is possible to traverse all datarows and set their parentid column to null before databinding RadMenu. Here is some simple code:

    foreach (DataRow row in myDataTable.Rows)
    {
        int parentID = (int)row["ParentID"];
        if (parentID == 0)
        {
          row["ParentID"] = null;
        }
    }

    Alternatively you can modify your database query so it returns NULL instead of 0 (for SQL server):

    use a query that returns the expected value (null). For example:

     SELECT ID, Text, IF(ParentID = 0, NULL, ParentID) FROM tblData


    Regards,
    Albert
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Stewart Ellis
    Stewart Ellis avatar
    21 posts
    Member since:
    Feb 2006

    Posted 03 Sep 2008 Link to this post

    Hey Albert, I'm trying to create a stored procedure that swaps in the null value on the parentId. Here's the code:

    SELECT

    menuId, text, url, target, sort, langId, IF(parentID = 0, NULL, parentID)
    FROM Menu
    WHERE (langId = @langId)
    ORDER BY sort

    VS2008 is not happy with that syntax though. When I try to compile the query it says:

    Incorrect syntax near the Keywork IF.
    Incorrect syntax near ','.

    I have also tried NullIf and that does not work either. Any ideas

  4. Jeremy T. Fuller
    Jeremy T. Fuller avatar
    51 posts
    Member since:
    Aug 2012

    Posted 03 Sep 2008 Link to this post

    Try this:

    1 SELECT 
    2     menuId, 
    3     text, 
    4     url, 
    5     target, 
    6     sort, 
    7     langId, 
    8     CASE WHEN parentID = 0 THEN NULL 
    9          ELSE parentID 
    10     END 
    11 FROM 
    12     Menu 
    13 WHERE 
    14     (langId = @langId) 
    15 ORDER BY 
    16     sort 

  5. Stewart Ellis
    Stewart Ellis avatar
    21 posts
    Member since:
    Feb 2006

    Posted 08 Sep 2008 Link to this post

    cool  - that worked...

    Thank you
Back to Top