P1ST0LPETE
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
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help!
ASKER
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