Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
MenuItem menu = new MenuItem();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("SP_DemoMenu", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();

            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(ds);
            con.Close();
          
           foreach (DataRowView item in mainMen)
            {
                MenuItem Mmnu = new MenuItem(item["MenuName"].ToString(),
                item["MenuId"].ToString(), "", item["Url"].ToString());
                mainMenu.Items.Add(Mmnu);
                subMenu.RowFilter = "MenuId = " + Mmnu.Value;
                foreach (DataRowView subItem in subMenu)
                {
                MenuItem sMnu = new MenuItem(subItem["SubMenuName"].ToString(), 
                subItem["SubMenuId"].ToString(), "", subItem["MenuUrl"].ToString());
                mainMenu.Items[Convert.ToInt32(Mmnu.Value)-1].ChildItems.Add(sMnu);
                subChildMenu.RowFilter = "MenuId = " + Mmnu.Value + " And SubMenuID = " + sMnu.Value;
                foreach (DataRowView childItem in subChildMenu)
                {
                MenuItem chldMnu = new MenuItem(childItem["ChildMenuName"].ToString(),"","", childItem["MenuUrl"].ToString());
            mainMenu.Items[Convert.ToInt32(Mmnu.Value) - 1].ChildItems[Convert.ToInt32(sMnu.Value) - 1].ChildItems.Add(chldMnu);
        }
    }    
}


Error Given

Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'mainMen' does not exist in the current context

Source Error:


Line 30:             con.Close();
Line 31:           
Line 32:            foreach (DataRowView item in mainMen)
Line 33:             {
Line 34:                 MenuItem Mmnu = new MenuItem(item["MenuName"].ToString(),
Posted
Updated 12-Oct-13 7:21am
v2

1 solution

Well, there is no mainMen (not the missing "u" from the end), but even no mainMenu declared in the code you posted. So it is quite normal that you get this error. And you will find nobody here able to guess how the rest of your code looks like.
But I am pretty sure, that the collection you are iterating in that foreach is not mainMen, neither mainMenu, since it is unlikely that something called "menu" is a collection of DataRowView elements.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900