2
Answers

this Code Can Error Given What proplem given in this code

krishna pandey

krishna pandey

11y
1.2k
1
 Connect();
        con.Open();
        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);
                }
        }
        }
    
        con.Close();



error given
 

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 55:                 if (dr["ParentId"] != null)
Line 56:                 {
Line 57:                     menuBar.FindItem(dr["ParentId"].ToString()).ChildItems.Add(mnu);
Line 58:                 }
Line 59:         }

database table
SELECT TOP 1000 [MenuId]
      ,[MenuName]
      ,[LocationUrl]
      ,[ParentId]
  FROM [SMSSystem].[dbo].[MenuControl]


Answers (2)