DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "Select * from MenuControl";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
dt = ds.Tables[0];
foreach (DataRow dr in dt.Select("ParentId=" + 0))
{
if (dr["MenuName"] != null && dr["MenuId"] != null && dr["LocationUrl"] != null)
{
menuBar.Items.Add(new MenuItem(dr["MenuName"].ToString(), dr["MenuId"].ToString(), "", dr["LocationUrl"].ToString()));
}
}
foreach (DataRow dr in dt.Select("ParentId>" + 0))
{
if (dr["MenuName"] != null && dr["MenuId"] != null && dr["LocationUrl"] != null)
{
MenuItem mnu = new MenuItem(dr["MenuName"].ToString(), dr["MenuId"].ToString(), "", dr["LocationUrl"].ToString());
if (dr["ParentId"] != null)
{
menuBar.FindItem(dr["ParentId"].ToString()).ChildItems.Add(mnu);
}
}
}
这是错误:
System.NullReferenceException: Object reference not set to an instance of an object.
if (dr["ParentId"] != null)
Line 56: {
Line 57: menuBar.FindItem(dr["ParentId"].ToString()).ChildItems.Add(mnu);
Line 58: }
Line 59: }