I can't seem to get this to work. I want to fill multiple dropdown lists with the same data. It is inefficient to run the same query each time. Here's where I've managed to get so far:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
Dim ddlArray = {lst1, lst2, lst3, lst4, lst5, lst6, lst7, lst8, lst9, lst10}
PopulateObjectList(ddlArray)
End If
End Sub
Sub PopulateObjectList(ByVal ddlArray As Array)
strSQL = "select distinct Object_ID, Object_Description from object"
objCmd = New SqlCommand(strSQL, Master.DBConnect())
For Each ddl In ddlArray
ddl.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
ddl.DataValueField = "Object_ID"
ddl.DataTextField = "Object_Description"
ddl.DataBind()
ddl.Items.Insert(0, "Select Object")
Next
Call Master.DBDisconnect()
End Sub
This of course gives the error:
ExecuteReader requires an open and available Connection. The connection's current state is closed.
because the connection is closed after the initial datasource is assigned. I would imagine I could instead create a variable to hold the data then bind it to each dropdown list, but dataset doesn't work and I'm not sure how else to do it.
How do I populate each of the ten dropdown lists with the same data without having to run ten separate queries on the database?