DropDownList selected values

D

Dylan Mines

Hi

I'm really confused about the behaviour of a drop down list that is
populated from a database table.
The problem that i was getting was the value (selected from my dropdown
list) was always being added to the database as '0' no matter what item was
selected.
To try and debug i wrote the selected value to a label at the onclick event
but i just get the 'object reference not set to an instance of an object'
error message. What does this error mean in this case?

My code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then

SqlDataAdapter1.Fill(DataSet1)

DropDownList1.DataSource = DataSet1

DropDownList1.DataTextField = "typeDescription"

DropDownList1.DataValueField = "typeCode"

DropDownList1.DataBind()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = DropDownList1.SelectedItem.Text

Label2.Text = DropDownList1.SelectedItem.Value

End Sub

The error occurs at the point where it attempts to write to the first label.

This code is as simple as it gets but i still get the error. What is going
on?

Can you also tell me why this error occurs so often and without any
explanation? I find it really frustrating because i see this error as
meaning 'there is an error and i'm not going to tell you what it is'. Any
help in how to diagnose this common error would be much appreciated.


Thanks,

Dylan
 
D

Dylan Mines

If i remove the 'if not postback' condition then the top item in the list is
always selected.

1. it is not maintaining viewstate
2. it is attempting to read the selected value on my onclick event after
posting back

what am i doing wrong?
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top