Object reference not set to an instance of an object.

Discussion in 'ASP .Net Web Controls' started by Seok Bee, May 10, 2006.

  1. Seok Bee

    Seok Bee Guest

    Hi All,

    I have a webform with a button to add record into the database. When the
    button is being clicked, the program will assign initial value controls in a
    detailsview control. When the first time, the button being click, the program
    works as expected. However, the next click on the button, the program showed
    the following error message:

    System.NullReferenceException was unhandled by user code
    Message="Object reference not set to an instance of an object."
    Source="App_Web_xogepfrv"
    StackTrace:
    at Inv_Maint.GetDefaultValue(String objMainType) in
    C:\Data\OITRS\Inv_Upd.aspx.vb:line 842
    at Inv_Maint.btnAddInventory_Click(Object sender, EventArgs e) in
    C:\Data\OITRS\Inv_Upd.aspx.vb:line 1132
    at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument)
    at
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    at System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


    Below is the code for the button:

    Protected Sub btnAddInventory_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnAddInventory.Click

    HideAll()
    btnAddInventory.Visible = False
    Select Case UCase(ddlMainType.SelectedItem.Value)
    Case "MONITOR"
    dvInventory_Monitor.Visible = True
    dvInventory_Monitor.ChangeMode(DetailsViewMode.Insert)

    Case "COMPUTER"
    dvInventory_Computer.Visible = True
    dvInventory_Computer.ChangeMode(DetailsViewMode.Insert)
    GetDefaultValue("dvInventory_Computer")

    .....

    Below is the the code for GetDefaultValue :
    Private Sub GetDefaultValue(ByVal objMainType As String)
    Select Case (objMainType)
    Case "dvInventory_Computer"
    CType(dvInventory_Computer.FindControl("txtMainType"),
    TextBox).Text = ddlMainType.SelectedValue.ToString
    CType(dvInventory_Computer.FindControl("ddlSubType"),
    DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
    CType(dvInventory_Computer.FindControl("txtUnitPrice"),
    TextBox).Text = "0.00"
    CType(dvInventory_Computer.FindControl("txtQuantity"),
    TextBox).Text = "1"
    CType(dvInventory_Computer.FindControl("ddlRAMType"),
    DropDownList).SelectedValue = "NONE"

    CType(dvInventory_Computer.FindControl("ddlNICManufacturer"),
    DropDownList).SelectedValue = "NONE"
    CType(dvInventory_Computer.FindControl("ddlOpticalDrive"),
    DropDownList).SelectedValue = "NONE"
    CType(dvInventory_Computer.FindControl("txtHDDSpace"),
    TextBox).Text = Nothing
    CType(dvInventory_Computer.FindControl("ddlGCManufacturer"),
    DropDownList).SelectedValue = "NONE"

    CType(dvInventory_Computer.FindControl("ddlModemManufacturer"),
    DropDownList).SelectedValue = "NONE"

    ....
    Please advise...


    Regards,
    SB
    Seok Bee, May 10, 2006
    #1
    1. Advertising

  2. Seok Bee

    sloan Guest

    The web is a stateless environment.

    Therefore, you dv is null (nothing in vb.net) when the page does a post
    back.


    You should write 3 methods for you page , something like this

    //member variable
    private DataView m_dv = null;

    private void LoadData()
    {
    this.m_dv = SomeMethodToFillTheDataView();
    }

    private void CacheData()
    {
    Session["myKeyName"] = this.m_dv;

    }

    private void RetrieveData()
    {
    if (null!=Session["myKeyName"])
    {
    //the data is in the Session, therefore use it
    this.m_dv = Session["MyKeyName"] as DataView;
    }
    else
    {
    //session data not found, load it fresh
    this.LoadData();
    }
    }

    Page_Load()
    {
    if (!Page.IsPostBack)
    {
    this.LoadData();
    this.CacheData();
    }
    }

    When you have a button click

    mybutton_click ( object sender , EventArgs e)
    {
    this.RetrieveData();

    //now do stuff with the DataView
    }


    The code is from memory, esp the button click thing. Don't rely on my args,
    let the IDE create the button_click for you.

    If you want to reuse data on a PostBack, you have to cache it, and then
    retrieve from the cache.

    spaces.msn.com/sholliday

    check that, as I have a more robust object storage mechanism .



    "Seok Bee" <> wrote in message
    news:...
    > Hi All,
    >
    > I have a webform with a button to add record into the database. When the
    > button is being clicked, the program will assign initial value controls in

    a
    > detailsview control. When the first time, the button being click, the

    program
    > works as expected. However, the next click on the button, the program

    showed
    > the following error message:
    >
    > System.NullReferenceException was unhandled by user code
    > Message="Object reference not set to an instance of an object."
    > Source="App_Web_xogepfrv"
    > StackTrace:
    > at Inv_Maint.GetDefaultValue(String objMainType) in
    > C:\Data\OITRS\Inv_Upd.aspx.vb:line 842
    > at Inv_Maint.btnAddInventory_Click(Object sender, EventArgs e) in
    > C:\Data\OITRS\Inv_Upd.aspx.vb:line 1132
    > at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
    > at System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    > eventArgument)
    > at
    >

    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
    stBackEvent(String eventArgument)
    > at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    > sourceControl, String eventArgument)
    > at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection

    postData)
    > at System.Web.UI.Page.ProcessRequestMain(Boolean
    > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    >
    >
    > Below is the code for the button:
    >
    > Protected Sub btnAddInventory_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btnAddInventory.Click
    >
    > HideAll()
    > btnAddInventory.Visible = False
    > Select Case UCase(ddlMainType.SelectedItem.Value)
    > Case "MONITOR"
    > dvInventory_Monitor.Visible = True
    > dvInventory_Monitor.ChangeMode(DetailsViewMode.Insert)
    >
    > Case "COMPUTER"
    > dvInventory_Computer.Visible = True
    > dvInventory_Computer.ChangeMode(DetailsViewMode.Insert)
    > GetDefaultValue("dvInventory_Computer")
    >
    > .....
    >
    > Below is the the code for GetDefaultValue :
    > Private Sub GetDefaultValue(ByVal objMainType As String)
    > Select Case (objMainType)
    > Case "dvInventory_Computer"
    > CType(dvInventory_Computer.FindControl("txtMainType"),
    > TextBox).Text = ddlMainType.SelectedValue.ToString
    > CType(dvInventory_Computer.FindControl("ddlSubType"),
    > DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
    > CType(dvInventory_Computer.FindControl("txtUnitPrice"),
    > TextBox).Text = "0.00"
    > CType(dvInventory_Computer.FindControl("txtQuantity"),
    > TextBox).Text = "1"
    > CType(dvInventory_Computer.FindControl("ddlRAMType"),
    > DropDownList).SelectedValue = "NONE"
    >
    > CType(dvInventory_Computer.FindControl("ddlNICManufacturer"),
    > DropDownList).SelectedValue = "NONE"
    > CType(dvInventory_Computer.FindControl("ddlOpticalDrive"),
    > DropDownList).SelectedValue = "NONE"
    > CType(dvInventory_Computer.FindControl("txtHDDSpace"),
    > TextBox).Text = Nothing
    >

    CType(dvInventory_Computer.FindControl("ddlGCManufacturer"),
    > DropDownList).SelectedValue = "NONE"
    >
    > CType(dvInventory_Computer.FindControl("ddlModemManufacturer"),
    > DropDownList).SelectedValue = "NONE"
    >
    > ....
    > Please advise...
    >
    >
    > Regards,
    > SB
    sloan, May 13, 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. Parthiv Joshi
    Replies:
    2
    Views:
    670
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,362
    Anushi
    Jul 29, 2004
  3. Chris Fink
    Replies:
    2
    Views:
    4,063
    David Waz...
    Jul 3, 2003
  4. yysiow
    Replies:
    1
    Views:
    418
    Kevin Spencer
    Jul 12, 2003
  5. Replies:
    1
    Views:
    464
    Mark Rae [MVP]
    Sep 20, 2007
Loading...

Share This Page