Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Object reference not set to an instance of an object.

Reply
Thread Tools

Object reference not set to an instance of an object.

 
 
Seok Bee
Guest
Posts: n/a
 
      05-10-2006
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.RaisePostBackEven t(String
eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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.Ins ert)

Case "COMPUTER"
dvInventory_Computer.Visible = True
dvInventory_Computer.ChangeMode(DetailsViewMode.In sert)
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("txtMainTyp e"),
TextBox).Text = ddlMainType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("ddlSubType "),
DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("txtUnitPri ce"),
TextBox).Text = "0.00"
CType(dvInventory_Computer.FindControl("txtQuantit y"),
TextBox).Text = "1"
CType(dvInventory_Computer.FindControl("ddlRAMType "),
DropDownList).SelectedValue = "NONE"

CType(dvInventory_Computer.FindControl("ddlNICManu facturer"),
DropDownList).SelectedValue = "NONE"
CType(dvInventory_Computer.FindControl("ddlOptical Drive"),
DropDownList).SelectedValue = "NONE"
CType(dvInventory_Computer.FindControl("txtHDDSpac e"),
TextBox).Text = Nothing
CType(dvInventory_Computer.FindControl("ddlGCManuf acturer"),
DropDownList).SelectedValue = "NONE"

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

....
Please advise...


Regards,
SB
 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      05-13-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.RaisePostBackEven t(String
> eventArgument)
> at
>

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
stBackEvent(String eventArgument)
> at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
> sourceControl, String eventArgument)
> at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection

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.Ins ert)
>
> Case "COMPUTER"
> dvInventory_Computer.Visible = True
> dvInventory_Computer.ChangeMode(DetailsViewMode.In sert)
> 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("txtMainTyp e"),
> TextBox).Text = ddlMainType.SelectedValue.ToString
> CType(dvInventory_Computer.FindControl("ddlSubType "),
> DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
> CType(dvInventory_Computer.FindControl("txtUnitPri ce"),
> TextBox).Text = "0.00"
> CType(dvInventory_Computer.FindControl("txtQuantit y"),
> TextBox).Text = "1"
> CType(dvInventory_Computer.FindControl("ddlRAMType "),
> DropDownList).SelectedValue = "NONE"
>
> CType(dvInventory_Computer.FindControl("ddlNICManu facturer"),
> DropDownList).SelectedValue = "NONE"
> CType(dvInventory_Computer.FindControl("ddlOptical Drive"),
> DropDownList).SelectedValue = "NONE"
> CType(dvInventory_Computer.FindControl("txtHDDSpac e"),
> TextBox).Text = Nothing
>

CType(dvInventory_Computer.FindControl("ddlGCManuf acturer"),
> DropDownList).SelectedValue = "NONE"
>
> CType(dvInventory_Computer.FindControl("ddlModemMa nufacturer"),
> DropDownList).SelectedValue = "NONE"
>
> ....
> Please advise...
>
>
> Regards,
> SB



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments