Object reference not set to an instance of an object.

B

bazzer

i have a webform with a dropdownlist and 2 textboxes. it has a dataset
with a table with 2 fields, 'areaID' and 'areaName'.

in the Data properties window of the DDL, i have the DataTextField set
to areaName, and the DataValueField set to areaID.

when an item is selected from the DDL i need the areaName to go in one
textbox and areaID to go in another.

this line to get the areaName works fine:
TextBox1.Text = DropDownList1.SelectedValue

but when i try get the areaID field:
TextBox1.Text = DropDownList1.SelectedItem.Text

i get the following error:
Object reference not set to an instance of an object.

can anyone tell me what exactly this error means and how to fix it
please? my head is wrecked from this.
 
G

Guest

Usually that error means that you have tried to assign a value to an object
that has been declared but not initialised e.g.

TextBox MyTextBox;
MyTextBox.Text = "SomeWords";
 
G

Gozirra

Nothing has been selected in the list.

DropDownList1.SelectedItem is null (nothing). SelectedValue will
return "" if nothing is selected so it appears to be working but is
probably not pulling the value you expect.

If you changed TextBox1.Text = DropDownList1.SelectedValue to
TextBox1.Text = DropDownList1.SelectedItem.Value, the exception should
move to this line.

The question is why is nothing selected? Have fun.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top