[newbie] need help on this NULL object reference

Discussion in 'ASP .Net' started by Jeff, Nov 11, 2006.

  1. Jeff

    Jeff Guest

    Hey

    asp.net 2.0

    Below is the code I have behind a web page. The problem is during runtime in
    btnSave_Click, userProfile has a NULL value. I don't understand why it has a
    NULL value, because AFAIK I've declared userProfile as a class variable so
    the value it has in btnSave_Click should be the value it did get in
    Page_Load. userProfile get a value in Page_Load - but is NULL in
    btnSave_Click

    What am I doing wrong here?

    public partial class EditUser : System.Web.UI.Page
    {
    private ProfileCommon userProfile;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    string username = Request.QueryString["user"].ToString();
    if (username.Length > 0)
    {
    userProfile = Profile.GetProfile(username);
    this.Settings1.SetText(userProfile);
    }

    }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
    this.Settings1.SaveInfo(userProfile);
    }
    }
     
    Jeff, Nov 11, 2006
    #1
    1. Advertising

  2. First, you always need to check your Request.QueryString variables to ensure
    they aren't null.

    You'll need to add an

    if(Request.QueryString["user"] != null)

    condition check.

    The thing to keep in mind, the userprofile is set when you first load the
    page. It is not loaded the second time because you're only loading it if it
    isn't a postback. Keep in mind, setting a variable in a web form is not like
    setting a variable in a windows form, the state is not maintained betwee
    postbacks unless you create a mechanism to save the state of the item. You
    could try saving the profile into the viewstate and then fetching it back on
    subsequent calls. Possibly by creating a property that looks something like:

    private ProfileCommon UserProfile
    {
    get{
    if(ViewState["userprofile"] != null)
    return (ProfileCommon)ViewState["userprofile"];
    else
    return null;
    }
    set
    {
    ViewState["userprofile"] = value;
    }
    }

    Will this work? I don't know. I haven't tried to do this one before and I'm
    not sure if the profile can be serialized into the viewstate or not. The
    bottom line though, unless you can find a way to maintain the state of the
    profile you'll have to remove the !Page.Ispostback check.


    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006


    "Jeff" <> wrote in message
    news:%...
    > Hey
    >
    > asp.net 2.0
    >
    > Below is the code I have behind a web page. The problem is during runtime
    > in btnSave_Click, userProfile has a NULL value. I don't understand why it
    > has a NULL value, because AFAIK I've declared userProfile as a class
    > variable so the value it has in btnSave_Click should be the value it did
    > get in Page_Load. userProfile get a value in Page_Load - but is NULL in
    > btnSave_Click
    >
    > What am I doing wrong here?
    >
    > public partial class EditUser : System.Web.UI.Page
    > {
    > private ProfileCommon userProfile;
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!this.IsPostBack)
    > {
    > string username = Request.QueryString["user"].ToString();
    > if (username.Length > 0)
    > {
    > userProfile = Profile.GetProfile(username);
    > this.Settings1.SetText(userProfile);
    > }
    >
    > }
    > }
    >
    >
    > protected void btnSave_Click(object sender, EventArgs e)
    > {
    > this.Settings1.SaveInfo(userProfile);
    > }
    > }
    >
     
    Mark Fitzpatrick, Nov 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    7,436
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    27,590
    Mike Schilling
    Mar 29, 2006
  3. Replies:
    1
    Views:
    518
    Mark Rae [MVP]
    Sep 20, 2007
  4. Cirene
    Replies:
    1
    Views:
    853
    Alexey Smirnov
    Jun 9, 2008
  5. putty
    Replies:
    1
    Views:
    273
    putty
    Apr 5, 2005
Loading...

Share This Page