Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on 

Object reference not set... asp 4

The list box in question is:
<asp:ListBox ID="lstEducation" runat="server" Width="205px">
                        <asp:ListItem >High School</asp:ListItem>
                        <asp:ListItem>Associate&#39;s Degree</asp:ListItem>
                        <asp:ListItem>Four-Year College</asp:ListItem>
                        <asp:ListItem>Masters</asp:ListItem>
                        <asp:ListItem>PhD</asp:ListItem>
                    </asp:ListBox>

Open in new window

This error occurs in page load at:

lstEducation.SelectedItem.Text = Session("education")

The intelesens shows a valid choice for the session variable.  
-------------------
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 62:             ddlMaterialStatus.SelectedItem.Text = Session("maritalStatus")
Line 63: 
Line 64:             lstEducation.SelectedItem.Text = Session("education")
Line 65:             chkAddComment.Checked = Session("addComment")
Line 66:             txtComment.Text = Session("comment")

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   addContact.Page_Load(Object sender, EventArgs e) in ... AddContact.aspx.vb:64
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Open in new window

ASP.NET.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Mike Eghtebas
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

Try moving your code in Page_LoadComplete. This ensures that the controls have been loaded.

Giannis
Avatar of Mike Eghtebas

ASKER

I was trying to do validation in client side. OnLoad will require postback.
Well, it hits on Page_Load, so this doesn't seem to be in client side.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Hi CodeCruiser,

This sort of masks the problem there is in my solution. After returning from Confirm page AddNew page. In the load event of AddNew page I have:

lstEducation.SelectedItem.Text = Session("education")

The session variable has some value in it but it doesn't deliver to lstEducation control.

Why lstEducation.SelectedItem.Text = Session("education") is not working?

Thank you
Avatar of Mike Eghtebas

ASKER

It works only if Selected="True" attribute is added:

  <asp:ListBox ID="lstEducation" runat="server" Width="205px">
                        <asp:ListItem Selected="True">High School</asp:ListItem>
                        <asp:ListItem>Associate&#39;s Degree</asp:ListItem>
                        <asp:ListItem>Four-Year College</asp:ListItem>
                        <asp:ListItem>Masters</asp:ListItem>
                        <asp:ListItem>PhD</asp:ListItem>
           </asp:ListBox>

Open in new window


Why it doesn't work with no Selected="True" in one of the items?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

lstEducation.SelectedItem = Session("education")
Avatar of Mike Eghtebas

ASKER

Error      10      Property 'SelectedItem' is 'ReadOnly'.      

I am getting this error.

I possibly need to cycle through the values and find index of some sort and then use the index value to select it.
Avatar of Mike Eghtebas

ASKER

lstEducation.SelectedValue = Session("education")

works ok.

Thanks.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo