我想display submenu dynamically from database
。但我在菜单栏中遇到错误。FindItem(dr["ParentMenuId"].ToString()).ChildItems.Add(mnu);
该对象引用未设置为对象的实例。我也是unable to display my submenu
。有什么错误请告诉我。
我的代码是:
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostBack)
getMenu();
}
private void getMenu()
{
SqlConnection con = new SqlConnection(@"Data Source=ST015\SQLEXPRESS;Initial Catalog=MyData;Integrated Security=True");
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "Select * from Categories";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
dt = ds.Tables[0];
// DataRow[] drowpar = dt.Select("ParentMenuId=" + 0);
foreach (DataRow dr in dt.Select("ParentMenuId=" + 0))
{
menuBar.Items.Add(new MenuItem(dr["MenuName"].ToString(),
dr["MenuId"].ToString(), "",
dr["MenuDescription"].ToString()));
}
foreach (DataRow dr in dt.Select("ParentMenuId >" + 0))
{
MenuItem mnu = new MenuItem(dr["MenuName"].ToString(),
dr["MenuId"].ToString(),
"", dr["MenuDescription"].ToString());
menuBar.FindItem(dr["ParentMenuId"].ToString()).ChildItems.Add(mnu);
}
con.Close();
}
}