Filling a drop down from a database?

  • Thread starter Thread starter Russ
  • Start date Start date
R

Russ

I have a drop down list on a web page that I pre-fill onload from a SQL
table using a SqlDataReader.

The line of code that fills the drop down looks like this

while(myReader.Read())
{
ddlCompany.Items.Add(new ListItem(myReader["CompanyName"].ToString(),
myReader["CompanyID"].ToString()))
}


Now my problem is I have labels that I want to pre-fill with the information
from the first Company record in the table, but I am not sure how to do
that. If a user selects a different company from the drop down then I can
easily change all the labels in a SelectedIndexChanged event, but how can I
get them on the load of the page?

I hope what I am asking makes sense. Basically what I need to do is capture
all the data from the first record that the reader gets and output it into
the labels.
 
Hey,

Check the DDLCompanyList.Items.Count. If it is > 0 then you have more than
one record

Once you are done loading data from reader in to the DDL, query the
list.count property and
you can always query whats in the list by DDL.items[0].Text &
DDL.items[0].Value
That should give you your first companys info....

Regards,

Hermit Dave
 
The sql query that I execute to fill the drop down returns all the fields
from the table but only uses 2 of them in the dropdown.

My page looks like this:

ddlCompany lblAdress lblCity lblState lblZip lblCountry

The dropdown shows the first Company but all the labels are blank. What I
need to figure out is how to pre-fill those labels. If I knew there was
only one record I could use code like this to do it:

myReader.Read()
lblAdress.Text = myReader["CompanyAddress"].ToString())
lblCity.Text = myReader["CompanyCity"].ToString())
lblState.Text = myReader["CompanyState"].ToString())
lblZip.Text = myReader["CompanyZip"].ToString())
lblCountry.Text = myReader["CompanyCountry"].ToString())

but in my case I have many records going into the drop down, but only need
the data from the very first one.
 
Well then y waste memory and processing time...
fetch two sets of results...
first call a procedure to get you the info for your DDL and from the return
query seperately for the first one
see when the user selects the item from ddl you will either way have to run
a query to fetch the results for that index...
might as well use the same query...

Regards,

Hermit Dave
 
Back
Top