public class MessageHelper : System.Web.UI.MasterPage
{
public MessageHelper()
{
}
public string Message
{
set { Session["Message"] = value; }
get
{
if (Session["Message"] != null)
{
var msg = Session["Message"] as string;
Session["Message"] = "";
return msg;
}
return "";
}
}
public string ErrorMsg
{
set { Session["Error"] = value; }
get
{
if (Session["Error"] != null)
{
var err = Session["Error"] as string;
Session["Error"] = "";
return err;
}
return "";
}
}
}
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.UserControl.get_Session() +15
WebApplication1.MessageHelper.get_ErrorMsg() in ..file.master.cs:71
where line 71 is: if (Session["Error"] != null)
what am I doing wrong here?!
EDIT (transcribed from original author):
@David,
here is AdminMaster.master.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
namespace WebApplication1
{
public partial class AdminMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
MessageHelper msg = new MessageHelper();
if (msg.ErrorMsg != "")
{
// do something
}
if (msg.ErrorMsg != "")
{
// do something
}
}
}
public class MessageHelper : System.Web.UI.MasterPage
{
public MessageHelper()
{
}
public string Message
{
set { System.Web.HttpContext.Current.Session["Message"] = value; }
get
{
if (System.Web.HttpContext.Current.Session["Message"] != null)
{
var msg = System.Web.HttpContext.Current.Session["Message"] as string;
System.Web.HttpContext.Current.Session["Message"] = "";
return msg;
}
return "";
}
}
public string ErrorMsg
{
set { System.Web.HttpContext.Current.Session["Error"] = value; }
get
{
if (System.Web.HttpContext.Current.Session["Error"] != null)
{
var err = System.Web.HttpContext.Current.Session["Error"] as string;
System.Web.HttpContext.Current.Session["Error"] = "";
return err;
}
return "";
}
}
}
}
so it does inherit from System.Web.UI.MasterPage, my bad.
i want the MessageHelper to be accessed from different pages on the site. all of my pages use the Master file, that's why i put the MessageHelper in the master file.
what is wrong here?
.ErrorMsg?