Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down List Problem

Status
Not open for further replies.

NeilV

Programmer
Oct 14, 2002
117
GB
Hello,

I am trying to create a drop down list that is populated by a database. I have down this many times before with no problems what so ever, but this time I am getting the following text in my dropdown list:
System.Data.Common.DbDatarecord

The code I am using is:

Dim Conn As SqlConnection

'SQL Connection for datagrid
'Dim SQLCommand1 As SqlCommand
Dim DataReader As SQlDataReader

Dim GetCostCenterSQL as String

'Create a connection to database
Conn = New SqlConnection(dbconstr)

'connection to the database
conn.open()

GetCostCenterSQL = myquery

Dim SQLCommand1 as SQlCommand = new SqlCommand GetCostCenterSQL, Conn)
DataReader = SQLCommand1.ExecuteReader(CommandBehavior.CloseConnection)

Ddl_CostCenter.DataSource = DataReader
Ddl_CostCenter.DataBind()

I cant see any problems with my code at all..but I'm guessing there is something very simple wrong!

Cheers,

NEil
 
Ok, I seem to have fixed this problem now, BUT how do I display the results of 2 fields in the drop down list? For example, if i have an ID number and a description I want to display both the ID number and description in the drop down list but set the value property to be just the ID number. Can this be done??

Neil
 
modify your query to pull the data back in that manner....

SELECT id, RTIM(CONVERT(varchar(10),id))+desc as desc FROM WHATEVER


 
Neil,

I have done this by combining the two fields in the sql statement.

Ex: Select ID + " - " + Description As IDDesc

Then bind IPDesc to your dropdown.
 
Cool! Cheers guys, i never knew about the + in SQL, that will be very useful!
 
Neil

I am attempting to do this exact same thing and I am getting the exact same values in the drop down box. How did you go about fixing this?

Jason Meckley
Database Analyst
WITF
 
The changes I made were to change this:

Dim SQLCommand1 as SQlCommand = new SqlCommand CostCenterSQL, Con)

To:

Dim SqlCom as New SqlCommand(CostCenterSQL,Con)

And add:

MyDropDown.DataValueField = "ID"
MyDropDown.DataTextField = "IDDesc"

before the Databind line.

Hope that helps.

Also i now wish to add a manual entry into the list. At the moment I can only get it to display at the bottom of the list but I would like it at the top. I am using:

MyDropDown.Items.Add("Choose From List..")
I have tried this line a tvarious points in the code but it either displays at the bottom or not at all.

NEil
 
I do not know the exact syntax but you are on the right path. I think you need to use the ItemIndex property to to add the value to a specific point in the list. I have seen posts on this delema before.

Jason Meckley
Database Analyst
WITF
 
Neil,

Do the following to insert into your dropdown. Do this after the databind. The first line inserts the item in at the beginning of the dropdown. The second line then sets the dropdown index to display the first value just inserted.

MyDropDown.Items.Insert(0,"Choose From List..")
MyDropDown.SelectedIndex = 0

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top