Add value to dropdown list AFTER it is populated dynamically.

aikeith

Well-known member
Joined
Jan 23, 2003
Messages
49
Could someone help me with the following issue.

I have a drop down list that gets its data from sql.

If I add the following code it works, but it adds the "Select Department" to the very end of the collection. I would like for it to be the first item in the dropdown list. If I move the "Select Department" before I call the databind method, it simply dissappears.

Thanks.....

If Page.IsPostBack = False Then
Try
myConnection.Open()
SweetDepts.DataSource = myCommand.ExecuteReader()
SweetDepts.DataBind()
SweetDepts.Items.Add("Select Department")
SweetDepts.Items.FindByText("Select Department").Value = "0"
SweetDepts.Items.FindByText("Select Department").Selected = True
myConnection.Close()
Catch e As Exception
SweetDepts.Items.Add("Departments Not Loaded")
SweetDepts.Items.FindByText("Departments Not Loaded").Value = 0
SweetDepts.Items.FindByText("Departments Not Loaded").Selected = True

End Try
End If
 
Have you tried doing the same way as you would in traditional ASP?

1. Open the reader
2. Add the "Select Department" to the drop down
3. Loop through the reader adding each record.

Code:
sqlDR = myCommand.ExecuteReader()
DropDownList1.Items.Add("Select Department")

While sqlDR.Read()
    DropDownList1.Items.Add(sqlDR("Department")
End While

John
 
That was it! I kept overlooking that because the tendancy it to do DropDown.Add

Thanks....
 
Back
Top