Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object

Status
Not open for further replies.

dpdoug

Programmer
Joined
Nov 27, 2002
Messages
455
Location
US
I'm trying to populate a bunch of controls in a form and I keep getting this error at line 206:

Code:
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 204:      Me.txtDescr.Text = dr("Descr")
Line 205:      Me.cboClasstype.SelectedIndex = -1
Line 206:      Me.cboClasstype.Items.FindByText(dr("Classtype")).Selected = True

Can anyone tell me what I am doing wrong?

I know the datareader is being populated, because it displays data in the text boxes when I test it. But when I include the DropdownLists in the test it errs out.


 
Hi dpdoug

A common cause of this error is trying to programatically access a protected control which does not have the runat="server" attribute in the aspx tag. I'd look there first as the exception is being thrown by a control.

Another common cause is trying to set or get a value from an object which hasn't (but requires) instantiating with the new operator.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
It may also be that .FindByText() isn't finding anything. If you assign its return value to a ListItem (then set it to "true"), it will be easier to examine and debug.
 
The prolem I found was the values that was being passsed to it were none of the ones found in the values list.

The values stored in the table were Null, A, I and N.

The Text in the dropdown were Active, Inactive and Not Available and the Values were the same. When I changed the values to Null, A, I, and N the problem went away since it was always finding a valid value in the dropdown list.

I needed it to initialize with a value of Null in case the user didn't have a valid value to store and I needed to leave the field Null. Also, if there was already a null value in the field it wouldn't err out for not finding a valid value in the values list.

I hope someone else can take advantage of this lesson learned.

Thanks for your everyone else's help.

David

"Do unto others as you would have them do unto you"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top