Reputation: 93
I have multiple drop down lists that I want to fill with the same items.
The following code will fill only the first DropDownList in the code (DropDownList1) and will not fill any of the others. For testing, I moved "DropDownList3" to be first in code to see what would happen and then only "DropDownList3" Filled with correct data. What do I need to do to make then all fill with the same data?
con.Open()
cmd1 = New SqlCommand(SqlQuery & SqlQuery2, con)
dr1 = cmd1.ExecuteReader
DropDownList1.DataSource = dr1
DropDownList1.DataTextField = "FullInfo"
DropDownList1.DataValueField = "FullInfo"
DropDownList1.DataBind()
DropDownList3.DataSource = dr1
DropDownList3.DataTextField = "FullInfo"
DropDownList3.DataValueField = "FullInfo"
DropDownList3.DataBind()
DropDownList4.DataSource = dr1
DropDownList4.DataTextField = "FullInfo"
DropDownList4.DataValueField = "FullInfo"
DropDownList4.DataBind()
DropDownList5.DataSource = dr1
DropDownList5.DataTextField = "FullInfo"
DropDownList5.DataValueField = "FullInfo"
DropDownList5.DataBind()
DropDownList6.DataSource = dr1
DropDownList6.DataTextField = "FullInfo"
DropDownList6.DataValueField = "FullInfo"
DropDownList6.DataBind()
DropDownList7.DataSource = dr1
DropDownList7.DataTextField = "FullInfo"
DropDownList7.DataValueField = "FullInfo"
DropDownList7.DataBind()
con.Close()
Upvotes: 0
Views: 20
Reputation: 35544
You can fill a DataTable
and reuse that as a source.
Dim dt As DataTable = New DataTable
Dim connection As SqlConnection = New SqlConnection(connStr)
Dim adapter As SqlDataAdapter = New SqlDataAdapter(SqlQuery & SqlQuery2, connection)
adapter.Fill(dt)
DropDownList1.DataSource = dt
DropDownList2.DataSource = dt
Upvotes: 1
Reputation: 16
private sub BindDrops(byRef drop as dropdownlist, byval text as string, byval value as string)
con.Open()
cmd1 = New SqlCommand(SqlQuery & SqlQuery2, con)
dr1 = cmd1.ExecuteReader
con.close()
drop.DataSource = dr1
drop.DataTextField = text
drop.DataValueField = value
drop.databind()
end sub
The query needs to return a text and a value: i.e: query = "SELECT ID_FIELD, NAME_FIELD FROM..."
BindDrops(DropDownList1, "NAME_FIELD","ID_FIELD")
BindDrops(DropDownList2, "NAME_FIELD","ID_FIELD")
...
BindDrops(DropDownListN, "NAME_FIELD","ID_FIELD")
Upvotes: 0