Object reference not set to an instance of an object.

S

Seok Bee

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
 
S

sloan

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 said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top