Saturday, March 31, 2012

System.IndexOutOfRangeException - DDL Binding

Hey

Im trying to populate a dropdown list for a holiday package selection
I want the drop down list to produce a list something like this:

Australia
- Sydney
- Gold Coast
- Canberra
Asia
- China
- Japan
etc etc

There seems to be something wrong with setting a string to a datafield
Particularly the line of code:

strDestination = myDataReader("Destinaton")

This is the error I get:

Exception Details: System.IndexOutOfRangeException: Destinaton

Here is the code:

Sub PopulateDestinationDDL()
Dim dlConnection as New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=c:\digitise\hot\database\hot80.mdb")
Dim dlCommandText as String = "SELECT DISTINCT Region, Destination FROM Deal ORDER BY Destination"
Dim dlCommand as New OleDbCommand(dlCommandText, dlConnection)

dlConnection.Open()
Dim myDataReader as OleDbDataReader = dlCommand.ExecuteReader()
While myDataReader.Read
Dim strDestination as String
If Not myDataReader("Destination") = strDestination
dlDestination.Items.Add(New ListItem(myDataReader("Destination"), myDataReader("Destination")))
End If
dlDestination.Items.Add(New ListItem("- " & myDataReader("Region"), myDataReader("Region")))
strDestination = myDataReader("Destinaton")
End While
dlConnection.Close()
End Sub 'PopulateDestinationDDL

Cheers
CharlesI think you mean to say Destination, rather than Destinaton. ;)
Yeah I accually just realised that as soon as I posted it. ;)
Silly me.

But I have another thing I am confused about....

With this drop down list I want it to autopost back and then change its items
I can't seem to get the drop down list to lose all of its items before repopulating it.

Cheers
Charles

Are you doing your databind on every postback? Are you calling PopulateDestinationDDL from your Page_Load sub procedure? Try wrapping it inside of aIf Not Page.IsPostback code block.

Please show your Page_Load code if you still want help.

Here is all the code I have made:


Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack Then
PopulateDestinationDDL()
End If
End Sub 'Page_Load

Sub PopulateDestinationDDL()
'dlDestination = New DropDownList()
Dim dlCommandText as String
If rdHolAir.SelectedItem.Value = "hol" Then
dlCommandText = "SELECT DISTINCT Region, Destination FROM Deal WHERE DealCode NOT LIKE '%HOT%' ORDER BY Destination"
Else
dlCommandText = "SELECT DISTINCT Region, Destination FROM Deal WHERE DealCode LIKE '%HOT%' ORDER BY Destination"
End If

Dim dlConnection as New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=lala.mdb")
Dim dlCommand as New OleDbCommand(dlCommandText, dlConnection)

dlConnection.Open()
Dim myDataReader as OleDbDataReader = dlCommand.ExecuteReader()
While myDataReader.Read
Dim strDestination as String
If Not myDataReader("Destination") = strDestination
dlDestination.Items.Add(New ListItem(myDataReader("Destination"), myDataReader("Destination")))
End If
dlDestination.Items.Add(New ListItem("- " & myDataReader("Region"), myDataReader("Region")))
strDestination = myDataReader("Destination")
End While
dlConnection.Close()
End Sub 'PopulateDestinationDDL

Sub rdHolAir_Change(sender as Object, e as EventArgs)
PopulateDestinationDDL()
End Sub 'rdHolAir_Change


Right - I think viewstate is automatically maintaining the values in your DropDownList. Try using the Items.Clear() method of your DDL before calling your PopulateDestinationDDL procedure.
Wait a second, you'll need to get the SelectedItem.Valuebefore clearing the items...
Shot
Works like a charm

Cheers
Charles

0 comments:

Post a Comment