Figuring out why a dropdownlist is considered empty

  • Thread starter Thread starter Plateriot
  • Start date Start date
P

Plateriot

Is there a more error proof way of deciding when to try:

Dim li As ListItem
li = Me.cmbSPEC.Items.FindByValue(Session("ssSpec"))

If li IsNot Nothing Then
li.Selected = True
end if


I figured out that I was receiving the error because my dropdown list was
empty
But now on this particular non-working instance, I have appropriately filled
my dropdown list manually in several pages including the one
I want it to work on... like this:


Private Sub FillSpecs()
With Me.cmbSPEC
.DataSource = Vitals_TB.PullSpecialties
.DataTextField = "Specialty"
.DataValueField = "DeptSpecID"

.DataBind()
End With

End Sub


Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then
PI = GetLoginData()
WP = GetWorkingProvider(Session("ssWorkingProvID"))


strNUID = PI.strNUID
SecLevel = PI.strSecLevel
Session("ssNUID") = strNUID

FillSpecs()



Dim li As ListItem
li = Me.cmbSPEC.Items.FindByValue(Session("ssSpec"))


If li IsNot Nothing Then
li.Selected = True
Else
'Start the Spec with the providers spec/dept
Session.Add("ssSpec", WP.strProvDept)

'Figure out why this line is throwing a null exception

Me.cmbSPEC.Items.FindByValue(Session("ssSpec")).Selected =
True
'I've mannually filled the combobox...
'I've checked the session variable, both are good -- so why
is it still throwing a null exception?

Me.Label1.Text = Session("ssSpec")
End If
End If

End Sub

it's strange because I have the same exact code on another page, but for
some reason its not working on this page -- what else can I examin to get to
the bottom of this?

Any reasonable suggestions would be greatly appreciated
 
You might have to put the combo's loading logic within Form_Init or
Form_LoadComplete. If you search the group for something like "Combo
forgetting values" you'll find where I got the idea from.
 
I tried exactly that - but I still get the same error.

I also tried finding the post using 'Combo forgetting values' or 'Forgetting
values' + Combo box - and the closest article I could find was 'Dynamic
Control Creation' betwee Philip and Sameer.

Sure, putting the combo's loading logic may work in a different event, but
explain why it works normally when I use the same exact logic on another page
that is exactly the same, with the exception of the combo box's name.

I've experienced a lot of this with Microsoft. Controls "forgetting" their
values... as if you have to 'Wake' them up to make them remember.

My MainFrame friend always laughs when he finds things that have a life of
their own in Visual Studio.
 
Back
Top