Link to home
Start Free TrialLog in
Avatar of P1ST0LPETE
P1ST0LPETEFlag for United States of America

asked on

Fill a multicolumn asp:ListBox from database.

Hi,
I've been searching the web for quite a while now looking for an example on how to do this.  I need to populate/fill a multicolumn asp:ListBox control from a database.  I understand that a ListBox control works quite similar to a DropDownList control.  I often use the attached code snippet example to fill my DDL's.  
However, with the ListBox I need to use the following SQL: "SELECT [FirstName], [LastName], [Email] FROM [Users].  Once I have the data pulled, and set as my datasource, how do I specify column 0 to hold FirstName, column 1 to hold Lastname and column 2 to hold Email on each row?

Thanks for the help,

Pete
string ddlSQL = "SELECT [Category] FROM [Letter_Categories]";
SqlCommand ddlCMD = new SqlCommand(ddlSQL, localConn);
localConn.Open();
SqlDataReader ddlDR = ddlCMD.ExecuteReader();
ddlLibraryCategories.DataSource = ddlDR;
ddlLibraryCategories.DataTextField = "Category";
ddlLibraryCategories.DataValueField = "Category";
ddlLibraryCategories.DataBind();
localConn.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of P1ST0LPETE

ASKER

Hmmmm.
I'm finding mutiple sources online that show that a listbox can be multicolumn, and how to hard code text values into each column.  I just can't determine how to populate each column from a database.  Here is a link showing a multicolmn listbox on msdn:
http://msdn.microsoft.com/en-us/library/bb176505.aspx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, my intensions are web based.  Thinking through things again, I am seeing that your original solution will work for what I need.  I do have a question though concerning the SQL statement.  How do I format the result of the columns being combined (i.e put a space between the first and last names)?  For example:

SELECT [FirstName] + [LastName] AS Usernames FROM [USERS]

Will give me results of names looking like: JonSmith
While I need names to look like: Jon Smith (space in between)

I've just forgotten the syntax on how to do this as it's been a while, and I can't seem to find it anywhere online.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help!