binding same data to two controls

D

Darrel

This is a followup to a previous question. I'm trying to grab some data and
bind it to two different dropdown lists. (same data in each one).

I can't seem to populate them both without resorting to what looks like two
separate queries:

Dim objConnect As New OleDb.OleDbConnection(strConnect)
objConnect.Open()
Dim objCommand As New System.Data.OleDb.OleDbCommand(strSQL, objConnect)
DropDownList_category1.DataSource = objCommand.ExecuteReader()
DropDownList_category1.DataTextField = "directoryCategoryName"
DropDownList_category1.DataValueField = "directoryCategoryID"
DropDownList_category1.DataBind()
objCommand.Dispose
Dim objConnect2 As New OleDb.OleDbConnection(strConnect)
objConnect2.Open()
Dim objCommand2 As New System.Data.OleDb.OleDbCommand(strSQL, objConnect2)
DropDownList_category2.DataSource = objCommand2.ExecuteReader()
DropDownList_category2.DataTextField = "directoryCategoryName"
DropDownList_category2.DataValueField = "directoryCategoryID"
DropDownList_category2.DataBind()
objCommand2.Dispose
objConnect.Close()

Is that, indeed, two separate trips to the DB? If so, is there a way to
write that so I'm only grabbing the data once? If I just use one connection
and one command, only the first DDL gets populated.

-Darrel
 
L

Lowell Heddings

Personally, I would grab the data once and store it in a DataSet, then
use the same dataset for box controls.

I'd also move all of my database layer code into a library, and just
call the object from whatever page you need to use it on. That
eliminates coding over and over.

Lowell
 
D

Darrel

Personally, I would grab the data once and store it in a DataSet, then use
the same dataset for box controls.

This might be a dumb question, but can I bind a datase directly to a drop
down list, or do I need to walk through each row of the DS and add it to the
DDL?
I'd also move all of my database layer code into a library, and just call
the object from whatever page you need to use it on. That eliminates
coding over and over.

Hmm...that's probably a good idea...although all these database calls are
fairly unique to each usercontrol I'm working on. I'm at the 'babysteps'
stage right now. ;o)

-Darrel
 
L

Lowell Heddings

Darrel said:
This might be a dumb question, but can I bind a datase directly to a drop
down list, or do I need to walk through each row of the DS and add it to the
DDL?


Yes, you can bind the dataset or datatable directly to the drop-down
list. That would generally be the preferred method of populating the list.

When you are binding to a datasource, be sure and watch where you are
binding it. You wouldn't want to necessarily re-bind on a postback,
because the viewstate will repopulate it for you, and track what has
been selected.

Lowell
 
D

Darrel

Yes, you can bind the dataset or datatable directly to the drop-down list.
That would generally be the preferred method of populating the list.

Duh. That WAS a dumb question...it's late. I'm not thinking straight.;o)

Thanks for rattling my brain. Got it working fine now!

Off to bed...

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top