locked
System.IndexOutOfRangeException - DDL Binding RRS feed

  • Question

  • User1470426624 posted
    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 Charles
    Sunday, September 28, 2003 7:45 PM

All replies

  • User1341174311 posted
    I think you mean to say Destination, rather than Destinaton. ;)
    Sunday, September 28, 2003 8:30 PM
  • User1470426624 posted
    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
    Sunday, September 28, 2003 8:33 PM
  • User1341174311 posted
    <color="black;width:500px;"> Are you doing your databind on every postback? Are you calling PopulateDestinationDDL from your Page_Load sub procedure? Try wrapping it inside of a If Not Page.IsPostback code block. Please show your Page_Load code if you still want help. </color>
    Sunday, September 28, 2003 8:39 PM
  • User1470426624 posted
    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
    Sunday, September 28, 2003 8:44 PM
  • User1341174311 posted
    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.
    Sunday, September 28, 2003 8:53 PM
  • User1341174311 posted
    Wait a second, you'll need to get the SelectedItem.Value before clearing the items...
    Sunday, September 28, 2003 8:54 PM
  • User1470426624 posted
    Shot Works like a charm Cheers Charles
    Sunday, September 28, 2003 9:13 PM