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

Binding RadMenu to SQL Database

4 Answers 163 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Flippie
Top achievements
Rank 1
Flippie asked on 22 Jun 2008, 04:42 PM
"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?

4 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 23 Jun 2008, 11:52 AM
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
0
Stewart Ellis
Top achievements
Rank 1
answered on 03 Sep 2008, 08:39 PM
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

0
Jeremy T. Fuller
Top achievements
Rank 1
answered on 03 Sep 2008, 10:04 PM
Try this:

SELECT 
    menuId, 
    text, 
    url, 
    target, 
    sort, 
    langId, 
    CASE WHEN parentID = 0 THEN NULL 
         ELSE parentID 
    END 
FROM 
    Menu 
WHERE 
    (langId = @langId) 
ORDER BY 
    sort 

0
Stewart Ellis
Top achievements
Rank 1
answered on 08 Sep 2008, 02:46 PM
cool  - that worked...

Thank you
Tags
Menu
Asked by
Flippie
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Stewart Ellis
Top achievements
Rank 1
Jeremy T. Fuller
Top achievements
Rank 1
Share this question
or