Friday, March 16, 2012

Object reference not set to an instance of an object.

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 progra
m
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,
SBWhich one is "line 842"?
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Hard work is a medication for which
there is no placebo.
"Seok Bee" <seokbee@.yahoo.com> wrote in message
news:FC2C952C-DAB3-4C99-9E9C-6C25578CD4C2@.microsoft.com...
> 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.Raise
PostBackEvent(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
Private Sub ShowError(ByVal strMainType As String, ByVal strField As
String)
Select Case UCase(strMainType)
Case "COMPUTER"
If strField = "Manufacturer" Then
CType(dvInventory_Computer.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Computer.FindControl("lblModel_ErrMsg"), Label).Visible =
True
ElseIf strField = "SubType" Then
CType(dvInventory_Computer.FindControl("lblSubType_ErrMsg"), Label).Visible
=
True
Else
CType(dvInventory_Computer.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_Computer.Visible = True
Case "NIC"
If strField = "Manufacturer" Then
CType(dvInventory_NIC.FindControl("lblManufacturer_ErrMsg"), Label).Visible
=
True
ElseIf strField = "Model" Then
CType(dvInventory_NIC.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_NIC.FindControl("lblSubType_ErrMsg"),
Label).Visible = True
Else
CType(dvInventory_NIC.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_NIC.Visible = True
Case "OPTICALDRIVE"
If strField = "Manufacturer" Then
CType(dvInventory_ExtOD.FindControl("lblManufacturer_ErrMsg"), Label).Visibl
e
= True
ElseIf strField = "Model" Then
CType(dvInventory_ExtOD.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_ExtOD.FindControl("lblSubType_ErrMsg"), Label).Visible =
True
Else
CType(dvInventory_ExtOD.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_ExtOD.Visible = True
Case "MONITOR"
If strField = "Manufacturer" Then
CType(dvInventory_Monitor.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Monitor.FindControl("lblModel_ErrMsg"), Label).Visible =
True
ElseIf strField = "SubType" Then
CType(dvInventory_Monitor.FindControl("lblSubType_ErrMsg"), Label).Visible =
True
Else
CType(dvInventory_Monitor.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_Monitor.Visible = True
Case "PRINTER/SCANNER"
If strField = "Manufacturer" Then
CType(dvInventory_PrinterScanner.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_PrinterScanner.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_PrinterScanner.FindControl("lblSubType_ErrMsg"),
Label).Visible = True
Else
CType(dvInventory_PrinterScanner.FindControl("lblKey_ErrMsg"), Label).Visibl
e
= True
End If
dvInventory_PrinterScanner.Visible = True
Case "UPS"
If strField = "Manufacturer" Then
CType(dvInventory_UPS.FindControl("lblManufacturer_ErrMsg"), Label).Visible
=
True
ElseIf strField = "Model" Then
CType(dvInventory_UPS.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_UPS.FindControl("lblSubType_ErrMsg"),
Label).Visible = True
Else
CType(dvInventory_UPS.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_UPS.Visible = True
Case "SOFTWARE"
If strField = "Manufacturer" Then
CType(dvInventory_Software.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Software.FindControl("lblModel_ErrMsg"), Label).Visible =
True
ElseIf strField = "SubType" Then
CType(dvInventory_Software.FindControl("lblSubType_ErrMsg"), Label).Visible
=
True
Else
CType(dvInventory_Software.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_Software.Visible = True
Case "NETWORKING"
If strField = "Manufacturer" Then
CType(dvInventory_Networking.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Networking.FindControl("lblModel_ErrMsg"), Label).Visible
=
True
ElseIf strField = "SubType" Then
CType(dvInventory_Networking.FindControl("lblSubType_ErrMsg"), Label).Visibl
e
= True
Else
CType(dvInventory_Networking.FindControl("lblKey_ErrMsg"), Label).Visible =
True
End If
dvInventory_Networking.Visible = True
Case "MODEM"
If strField = "Manufacturer" Then
CType(dvInventory_Modem.FindControl("lblManufacturer_ErrMsg"), Label).Visibl
e
= True
ElseIf strField = "Model" Then
CType(dvInventory_Modem.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_Modem.FindControl("lblSubType_ErrMsg"), Label).Visible =
True
Else
CType(dvInventory_Modem.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_Modem.Visible = True
Case "ACCESSORIES"
If strField = "Manufacturer" Then
CType(dvInventory_Accessories.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Accessories.FindControl("lblModel_ErrMsg"), Label).Visible
= True
ElseIf strField = "SubType" Then
CType(dvInventory_Accessories.FindControl("lblSubType_ErrMsg"),
Label).Visible = True
Else
CType(dvInventory_Accessories.FindControl("lblKey_ErrMsg"), Label).Visible =
True
End If
dvInventory_Accessories.Visible = True
Case "STORAGE"
If strField = "Manufacturer" Then
CType(dvInventory_Storage.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Storage.FindControl("lblModel_ErrMsg"), Label).Visible =
True
ElseIf strField = "SubType" Then
CType(dvInventory_Storage.FindControl("lblSubType_ErrMsg"), Label).Visible =
True
Else
CType(dvInventory_Storage.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
dvInventory_Storage.Visible = True
Case "OTHERS"
If strField = "Manufacturer" Then
CType(dvInventory_Others.FindControl("lblManufacturer_ErrMsg"),
Label).Visible = True
ElseIf strField = "Model" Then
CType(dvInventory_Others.FindControl("lblModel_ErrMsg"),
Label).Visible = True
ElseIf strField = "SubType" Then
CType(dvInventory_Others.FindControl("lblSubType_ErrMsg"), Label).Visible =
True
Else
CType(dvInventory_Others.FindControl("lblKey_ErrMsg"),
Label).Visible = True
End If
Line 842: dvInventory_Others.Visible = True
End Select
End Sub
"Kevin Spencer" wrote:

> Which one is "line 842"?
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> Professional Numbskull
> Hard work is a medication for which
> there is no placebo.
> "Seok Bee" <seokbee@.yahoo.com> wrote in message
> news:FC2C952C-DAB3-4C99-9E9C-6C25578CD4C2@.microsoft.com...
>
>
Interesting. So, you're telling me that *all* of this is "line 842"?
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Hard work is a medication for which
there is no placebo.
"Seok Bee" <seokbee@.yahoo.com> wrote in message
news:C061723B-3DC1-42EA-A392-B88E8B3F092C@.microsoft.com...
> Private Sub ShowError(ByVal strMainType As String, ByVal strField As
> String)
> Select Case UCase(strMainType)
> Case "COMPUTER"
> If strField = "Manufacturer" Then
> CType(dvInventory_Computer.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Computer.FindControl("lblModel_ErrMsg"), Label).Visible
> =
> True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Computer.FindControl("lblSubType_ErrMsg"),
> Label).Visible =
> True
> Else
> CType(dvInventory_Computer.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_Computer.Visible = True
> Case "NIC"
> If strField = "Manufacturer" Then
> CType(dvInventory_NIC.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible =
> True
> ElseIf strField = "Model" Then
> CType(dvInventory_NIC.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_NIC.FindControl("lblSubType_ErrMsg"),
> Label).Visible = True
> Else
> CType(dvInventory_NIC.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_NIC.Visible = True
> Case "OPTICALDRIVE"
> If strField = "Manufacturer" Then
> CType(dvInventory_ExtOD.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible
> = True
> ElseIf strField = "Model" Then
> CType(dvInventory_ExtOD.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_ExtOD.FindControl("lblSubType_ErrMsg"), Label).Visible =
> True
> Else
> CType(dvInventory_ExtOD.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_ExtOD.Visible = True
> Case "MONITOR"
> If strField = "Manufacturer" Then
> CType(dvInventory_Monitor.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Monitor.FindControl("lblModel_ErrMsg"), Label).Visible =
> True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Monitor.FindControl("lblSubType_ErrMsg"), Label).Visible
> =
> True
> Else
> CType(dvInventory_Monitor.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_Monitor.Visible = True
> Case "PRINTER/SCANNER"
> If strField = "Manufacturer" Then
> CType(dvInventory_PrinterScanner.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_PrinterScanner.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_PrinterScanner.FindControl("lblSubType_ErrMsg"),
> Label).Visible = True
> Else
> CType(dvInventory_PrinterScanner.FindControl("lblKey_ErrMsg"),
> Label).Visible
> = True
> End If
> dvInventory_PrinterScanner.Visible = True
> Case "UPS"
> If strField = "Manufacturer" Then
> CType(dvInventory_UPS.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible =
> True
> ElseIf strField = "Model" Then
> CType(dvInventory_UPS.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_UPS.FindControl("lblSubType_ErrMsg"),
> Label).Visible = True
> Else
> CType(dvInventory_UPS.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_UPS.Visible = True
> Case "SOFTWARE"
> If strField = "Manufacturer" Then
> CType(dvInventory_Software.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Software.FindControl("lblModel_ErrMsg"), Label).Visible
> =
> True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Software.FindControl("lblSubType_ErrMsg"),
> Label).Visible =
> True
> Else
> CType(dvInventory_Software.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_Software.Visible = True
> Case "NETWORKING"
> If strField = "Manufacturer" Then
> CType(dvInventory_Networking.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Networking.FindControl("lblModel_ErrMsg"),
> Label).Visible =
> True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Networking.FindControl("lblSubType_ErrMsg"),
> Label).Visible
> = True
> Else
> CType(dvInventory_Networking.FindControl("lblKey_ErrMsg"), Label).Visible
> =
> True
> End If
> dvInventory_Networking.Visible = True
> Case "MODEM"
> If strField = "Manufacturer" Then
> CType(dvInventory_Modem.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible
> = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Modem.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Modem.FindControl("lblSubType_ErrMsg"), Label).Visible =
> True
> Else
> CType(dvInventory_Modem.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_Modem.Visible = True
> Case "ACCESSORIES"
> If strField = "Manufacturer" Then
> CType(dvInventory_Accessories.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Accessories.FindControl("lblModel_ErrMsg"),
> Label).Visible
> = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Accessories.FindControl("lblSubType_ErrMsg"),
> Label).Visible = True
> Else
> CType(dvInventory_Accessories.FindControl("lblKey_ErrMsg"), Label).Visible
> =
> True
> End If
> dvInventory_Accessories.Visible = True
> Case "STORAGE"
> If strField = "Manufacturer" Then
> CType(dvInventory_Storage.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Storage.FindControl("lblModel_ErrMsg"), Label).Visible =
> True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Storage.FindControl("lblSubType_ErrMsg"), Label).Visible
> =
> True
> Else
> CType(dvInventory_Storage.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> dvInventory_Storage.Visible = True
> Case "OTHERS"
> If strField = "Manufacturer" Then
> CType(dvInventory_Others.FindControl("lblManufacturer_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "Model" Then
> CType(dvInventory_Others.FindControl("lblModel_ErrMsg"),
> Label).Visible = True
> ElseIf strField = "SubType" Then
> CType(dvInventory_Others.FindControl("lblSubType_ErrMsg"), Label).Visible
> =
> True
> Else
> CType(dvInventory_Others.FindControl("lblKey_ErrMsg"),
> Label).Visible = True
> End If
> Line 842: dvInventory_Others.Visible = True
> End Select
> End Sub
>
> "Kevin Spencer" wrote:
>
No, he actually marked the line with "Line 842:". I just saw it by
chance. It's the third line from the end in the large chunk of code.
Kevin Spencer wrote:
> Interesting. So, you're telling me that *all* of this is "line 842"?
>
Okay. That line reads:
dvInventory_Others.Visible = True
Apparently, dvInventory_Others is null (Nothing).
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Hard work is a medication for which
there is no placebo.
"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> wrote in message
news:ODcvfcEdGHA.3888@.TK2MSFTNGP02.phx.gbl...
> Interesting. So, you're telling me that *all* of this is "line 842"?
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> Professional Numbskull
> Hard work is a medication for which
> there is no placebo.
> "Seok Bee" <seokbee@.yahoo.com> wrote in message
> news:C061723B-3DC1-42EA-A392-B88E8B3F092C@.microsoft.com...
>

0 comments:

Post a Comment