dstewart101
dstewart101

Reputation: 1112

binding dropdownlist to array asp.net vb

I've hit a snag with the following code.

Dim dates As New ArrayList()
   Dim BWE1 As New ListItem(FirstBWEEndDateTextBox.Text, FirstBWEEndDateTextBox.Text)
   Dim BWE2 As New ListItem(SecondBWEEndDateTextBox.Text, SecondBWEEndDateTextBox.Text)
   Dim BWE3 As New ListItem(txtThirdBWEDate.Text, txtThirdBWEDate.Text)

   dates.Add(BWE1)
   dates.Add(BWE2)
   dates.Add(BWE3)

   STBAEndDateDDL.DataSource = dates
   STBAEndDateDDL.DataBind()

At the line...

STBAEndDateDDL.DataSource = dates

It's telling me that there is a null reference exception for dates. Totally stumped by this. It has been a long day, but surely there is enough in my code for the Visual Studio to know what dates is. No?

Any thoughts anyone? Many thanks. DS

Upvotes: 1

Views: 1554

Answers (1)

Josh Darnell
Josh Darnell

Reputation: 11433

Given your code above, this line:

STBAEndDateDDL.DataSource = dates

couldn't possibly be throwing a NullReferenceException for dates - you clearly initialized and populated it properly.

The other possibility is that STBAEndDateDDL (presumably a DropDownList) is null, because your in a stage of the Page life cycle where it doesn't exist / hasn't been initiliazed yet. That seems like the most likely culprit.

Upvotes: 1

Related Questions