Srange Object error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris

    Srange Object error

    Hi tere, I have a seperate class called Author:

    using System;

    namespace DocumentControl
    {
    /// <summary>
    /// Summary description for Author.
    /// </summary>
    public class Author
    {
    private string strReport = "";
    private string strIssue = "";
    private string strAuthors = "";

    public Author(string strReport, string strIssue, string strAuthors)
    {
    this.strReport = strReport;
    this.strIssue = strIssue;
    this.strAuthors = strAuthors;
    }

    public string StrReports
    {
    set
    {
    strReport = value;
    }
    get
    {
    return strReport;
    }
    }

    public string StrIssue
    {
    set
    {
    strIssue = value;
    }
    get
    {
    return strIssue;
    }
    }

    public string StrAuthor
    {
    set
    {
    strAuthors = value;
    }
    get
    {
    return strAuthors;
    }
    }


    }
    }

    I declare the object in the parent class:

    private Author objAuthor = null;

    I create the object on a Button Event whcih works fine and the values are in
    the object.

    private void AddAuthor(objec t source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    objAuthor = new Author(e.Item.C ells[2].Text, e.Item.Cells[6].Text,
    e.Item.Cells[12].Text);
    }

    Now I try to access the object from a different Button Click event:

    private void ButtonAddAuthor _Click(object sender, System.EventArg s e)
    {
    TextBox3.Text = objAuthor.StrRe ports;
    TextBox4.Text = objAuthor.StrIs sue;
    TextBox5.Text = objAuthor.StrAu thor;
    }

    But I get the error: Object reference not set to an instance of an object

    Why can't I access the objects content? What is wrong? Thanks a lot for any
    feedback

    Chris
  • Dmytro Lapshyn [MVP]

    #2
    Re: Srange Object error

    Hi Chris,

    The Web page class is not retained between subsequent HTTP requests. When
    you first click the button and thus make a postback to the server, your
    code-behind class is instantiated, an instance of the Author class gets
    initialized in the event handler, but as long as this request is processed,
    the whole instance of the code-behind class is recycled.

    When you click the other button, a completely new instance of the
    code-behind class is created, in which objAuthor is initialized to the null
    reference, thus giving you the error.

    If you need to retain an instance of a class between requests, you can store
    it in the Session object (will work on per-used basis), in the Application
    object, or in the Cache. Each scenario has its advantages and drawbacks, I
    think you should refer to ASP .NET MSDN docs for more details.

    --
    Sincerely,
    Dmytro Lapshyn [Visual Developer - Visual C# MVP]


    "chris" <chris@discussi ons.microsoft.c om> wrote in message
    news:62A891E0-D94D-4497-B696-180966B1B541@mi crosoft.com...[color=blue]
    > Hi tere, I have a seperate class called Author:
    >
    > using System;
    >
    > namespace DocumentControl
    > {
    > /// <summary>
    > /// Summary description for Author.
    > /// </summary>
    > public class Author
    > {
    > private string strReport = "";
    > private string strIssue = "";
    > private string strAuthors = "";
    >
    > public Author(string strReport, string strIssue, string strAuthors)
    > {
    > this.strReport = strReport;
    > this.strIssue = strIssue;
    > this.strAuthors = strAuthors;
    > }
    >
    > public string StrReports
    > {
    > set
    > {
    > strReport = value;
    > }
    > get
    > {
    > return strReport;
    > }
    > }
    >
    > public string StrIssue
    > {
    > set
    > {
    > strIssue = value;
    > }
    > get
    > {
    > return strIssue;
    > }
    > }
    >
    > public string StrAuthor
    > {
    > set
    > {
    > strAuthors = value;
    > }
    > get
    > {
    > return strAuthors;
    > }
    > }
    >
    >
    > }
    > }
    >
    > I declare the object in the parent class:
    >
    > private Author objAuthor = null;
    >
    > I create the object on a Button Event whcih works fine and the values are
    > in
    > the object.
    >
    > private void AddAuthor(objec t source,
    > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    > {
    > objAuthor = new Author(e.Item.C ells[2].Text, e.Item.Cells[6].Text,
    > e.Item.Cells[12].Text);
    > }
    >
    > Now I try to access the object from a different Button Click event:
    >
    > private void ButtonAddAuthor _Click(object sender, System.EventArg s e)
    > {
    > TextBox3.Text = objAuthor.StrRe ports;
    > TextBox4.Text = objAuthor.StrIs sue;
    > TextBox5.Text = objAuthor.StrAu thor;
    > }
    >
    > But I get the error: Object reference not set to an instance of an object
    >
    > Why can't I access the objects content? What is wrong? Thanks a lot for
    > any
    > feedback
    >
    > Chris[/color]

    Comment

    Working...