Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   HomeNew.Connect() +72
   HomeNew.getMenu() +20
   HomeNew.Page_Load(Object sender, EventArgs e) +36
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034 




My Master Page is................
C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class HomeNew : System.Web.UI.MasterPage
{
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getMenu();
        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {


        if (Session["UserName"] != null)
        {
            //Response.Redirect("~/Home.aspx");
            Label2.Text = Session["UserName"].ToString();
            
        }
        else
        {
            Response.Redirect("~/Default.aspx", true);
        }
    }
    private void getMenu()
    {
        //Connect();
        con.Open();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        string sql = "Select * from MenuItem";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        da.Fill(ds);
        dt = ds.Tables[0];
        DataRow[] drowpar = dt.Select("ParentID=" + 0);

        foreach (DataRow dr in drowpar)
        {
            menuBar.Items.Add(new MenuItem(dr["MenuName"].ToString(),
                    dr["MenuID"].ToString(), "",
                    dr["Location"].ToString()));
        }




        foreach (DataRow dr in dt.Select("ParentID >" + 0))
        {
            MenuItem mnu = new MenuItem(dr["MenuName"].ToString(),
                           dr["MenuID"].ToString(),
                           "", dr["Location"].ToString());
            menuBar.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu);
        }
        con.Close();
    }
    //public void Connect()
    //{
    //    con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        
    //}
    protected void lntnlogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("~/Default.aspx");
    }
}
Posted
Updated 21-Oct-13 2:52am
v2
Comments
Richard C Bishop 21-Oct-13 16:22pm    
What line is it failing at?

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