473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

binding same data to two controls

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
Nov 19 '05 #1
4 1208
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

Darrel wrote:
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

Nov 19 '05 #2
> 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
Nov 19 '05 #3
Darrel wrote:
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?

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

Nov 19 '05 #4
> 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
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
1
by: matty.hall | last post by:
There's a lot of information out there about data-binding UI objects (i.e. derived from Control) to non-UI custom business objects. Is it possible to do the same without any UI being involved at...
0
by: popsovy | last post by:
Hi I have a question about whether Data Binding can facilitate the process of saving data in a web application I learned that you can data bind information from a number of different data...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
11
by: Rourke Eleven | last post by:
I have looked and searched. What good is the databind property on Radiobuttons? How does one go about actually using it? What is a good resource on this? I understand that I can easily get/set...
0
by: Larry Serflaten | last post by:
I am not sure how many are aware of this sort of data binding, but as it is new to many (classic) VB developers I thought I would post this once just to let people know of its availablility. ...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.