views:

32

answers:

1
 XElement xml = new XElement("MyMenu",
                     from c in db.Security_Module_Menus
                     //where (c.ParentID == 0)
                     orderby c.Menu_ID
                     select new XElement("Item",
                               new XAttribute("Text", c.Menu_Name), new XAttribute("NavigateUrl", c.Target_URL)


                               )
                     ); 

From my above syntax c.Menu_Name,c.Target_URL values are nullable.Show me the error

Value cannot be null. Parameter name: value I know SetElementValue() used to solve this error.But how can i use to my above syntax.Help me to Use it.Show me syntax

A: 

Presumably you want to avoid creating the attributes if c.Menu_Name and c.Target_URL are null? If so, you can do it as follows:

new XElement("MyMenu",
  from c in db.Security_Module_Menus
  orderby c.Menu_ID
  select new XElement("Item",
    c.Menu_Name == null ? null : new XAttribute("Text", c.Menu_Name),
    c.Target_URL == null ? null : new XAttribute("NavigateUrl", c.Target_URL))
  )
Joe Albahari
Thanks for help .I have Menu_ID and Parent_ID columns.want to create <item>...<subitem>.....<sub-subitem>...on basis of parent id.http://stackoverflow.com/questions/3412530/how-to-create-dynamic-aspxmenu-from-database .Here you can see my talbe.Plz help me to solve this proble.Thanks again for help.
shamim