protected void Page_Load(object sender, EventArgs e)
        {
            if (CookieWrapper.Username != null && CookieWrapper.Role == "Member")
            {
                hlUsername.Text = CookieWrapper.Username;
                showProfilePicture();
            }
            else
                Response.Redirect("~/ErrorPages/401.aspx");            
        }

In this master page Page_Load event i will check whether the cookies values,
if false will redirect to the error page.
So another words, my child pages also will fire this MasterPage Page_Load event right.
But now my problem is occured, some of the child pages did fire this event, but some does not and result in "Object Reference Null"
And ideas?

Recommended Answers

I have had a similar problem where I had to move some code from page_load to another event in the page cycle, say Page.LoadComplete Event

Jump to Post

All 4 Replies

I have had a similar problem where I had to move some code from page_load to another event in the page cycle, say Page.LoadComplete Event

Which mean i just need to move the Page_Load event code from Master Page to Page.LoadComplete Event?

I couldnt say for sure without working with your code. I just wanted to mention to you that I had a similar problem where I had code in the Page_Load method in the Master and "child" pages and encountered an issue where I needed to ensure that one page_load block had to exectute before code in the other page_load block. My solution was to place the code that needed to run after in LoadComplete.

This may not be related to your situation, but I thought I'd metion it to you because your description seemed to be similar to the issue I encountered.

    public partial class MonthlyBudget_Report : System.Web.UI.Page
    {
        private double totalUsage = 0;
        private DateTime dtSelected = Convert.ToDateTime("1/1/1973");
        private string strUsername = CookieWrapper.Username;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                setDropDownList();
            dtSelected = Convert.ToDateTime("1/" + ddlMonth.SelectedValue + "/" + ddlYear.SelectedItem.Text);
            insertChart(getColumnName(), getValues());
            lblLoanAmt.Text = "RM " + totalUsage.ToString("n2");
        }

        protected void setDropDownList()
        {
            SqlConnection conSet = new SqlConnection(ConfigurationManager.ConnectionStrings["connMSJ"].ConnectionString);
            SqlCommand cmdSet = new SqlCommand("SELECT CurrentMonth FROM MonthlyBudget WHERE Username=@username", conSet);
            cmdSet.Parameters.AddWithValue("@username", strUsername);
            conSet.Open();
            SqlDataReader dtrSet = cmdSet.ExecuteReader();
            while (dtrSet.Read())
            {
                string CurrentMonth = dtrSet[0].ToString();
                int dtYear = Convert.ToDateTime(CurrentMonth).Year;
                ListItem liYear = ddlYear.Items.FindByText(dtYear.ToString());
                if(liYear == null)
                    ddlYear.Items.Add(dtYear.ToString());
            }
            dtrSet.Close();
            conSet.Close();
        }

This is the code where is meet the error "Object Reference Null". My cookies value is null. Any idea with this code?

It is okay if i add one more time check cookies value in the this child page Page_Load event, but if i don't want add extra check cookie value? any tricks?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.