开发者

NullReferenceException when assigning a Session variable/value

开发者 https://www.devze.com 2023-03-06 11:32 出处:网络
I have in many places inmy ASP.NET project used the Session variable for storing data. I usually write something like this:

I have in many places in my ASP.NET project used the Session variable for storing data. I usually write something like this:

public uint MyPropery 
{
    get 
    {
        object o = Session["MyProperty"];
        if (o != null)
            return (uint)o;
        else
            return 0;
    }
    set 
    {
        Session["MyProperty"] = value;
    }
}

However, this time I get a NullReferenceException in the setter. As far as I know, it is valid to assign the Session variable in the manner above. Also, Session is not null and neither is value.

Any ideas on this?

Edit:

Adding the code for the UserControl in which the property exists. I am using ext.net but that shouldn't have anything to do with this. One thought that crossed my mind:

The UserControl (seen below) is added dynamically in code-behind of a page. Can that have anything to do with it?

I am adding UserControls like this (on a Page):

foreach(CoreCommons.System.Comment c in cg.Reply_Comments)
{
    WebApplicationExtNetTest.Secure.UserControls.CoreComment cc = new UserControls.CoreComment();
    cc._Comment = c; // here is where i get the NullRef
    this.Panel1.ContentControls.Add(cc);
}

Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CoreComment.ascx.cs" Inherits="WebApplicationExtNetTest.Secure.UserControls.CoreComment" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<ext:Panel runat="server" ID="CoreCommentOuterPanel" BodyStyle="background: #FFFDDE">
    <Items>
        <ext:ColumnLayout runat="server">
            <Columns>
                <ext:LayoutColumn ColumnWidth="0.8">
                    <ext:Image runat="server" ImageUrl="/Resources/bullet_triangle_green_16x16.png" Align="AbsMiddle"></ext:Image> 
                    <ext:Label runat="server" ID="lblCommentInfo"></ext:Label>
                </ext:LayoutColumn>
                <ext:LayoutColumn ColumnWidth="0.2"><ext:Button runat="server" ID="btnDelete" Icon="Delete"></ext:Button></ext:LayoutColumn>
            </Columns>
        </ext:ColumnLayout>
        <ext:Label runat="server" ID="lblComment"></ext:Label>
    </Items>
</ext:Panel>

Code-behind:

namespace WebApplicationExtNetTest.Secure.UserControls
{
    public partial class CoreComment : System.Web.UI.UserControl
    {
        public CoreCommons.System.Comment _Comment
        {
            get
            {
                object o = Session["CoreComment_ObjectId"];
                if (o != null)
                    return (tW开发者_StackOverfloworks.Core.CoreCommons.System.Comment)o;
                else
                    return null;
            }
            set
            {
                Session["CoreComment_ObjectId"] = value;
                SetComment();
            }
        }            

        protected void Page_Load(object sender, EventArgs e)
        {    
        }

        private void SetComment()
        {
            if (_Comment == null)
            {
                lblCommentInfo.Text = "";
                lblComment.Text = "";
            }
            else
            {
                lblCommentInfo.Text = _Comment.Author + ", " + _Comment.TimeStamp.ToString("g");
                lblComment.Text = _Comment.Text;
            }
        }
    }
}


I'm almost completely sure the NullReferenceException is thrown in SetComment() because none of the CoreComment's child controls (lblComment, lblCommentInfo) are properly instantiated at the point you set the _Comment property.

The reason these child controls are not instantiated is indeed the way you currently add the CoreComment controls. For dynamically adding UserControls, you must use Page.LoadControl() (see: here) to create a new instance of the control, as it does some behind-the-scenes magic to ensure it is properly initialized, which includes the instantiation of the child controls.

On a sidenote, personally I'd change SetComment() to SetComment(CoreCommons.System.Comment comment) and use the parameter instead of repeatedly calling the getter, or, if staying with the original, at least call the getter only once and store the result in a local variable. With what I assume is probably InProc session storage it won't make much of a difference, but in any other storage mode you'd repeatedly deserialize the Comment object for no reason.


You need to use the Page.LoadControl() method instead , please look here

BTW:the problem is in adding the control programatically with that way.


Use:

return Session["MyProperty"] as uint? ?? 0;

and post somewhere full exception stack trace with inner exception(s)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号