473,387 Members | 1,899 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,387 software developers and data experts.

Best way to populate ListBox from SQL Server?

I have a SQL Server table called "ClosedMonths" that contains two pertinent
fields:

Yearx Monthx
2000 1
2000 2
2000 3
I want to simply query this table and populate a WebControls listbox with
the contents, though in this format:

Jan 2000
Feb 2000
Mar 2000
So I wrote the following code:

DataSet dataSet = new DataSet();
dataSet.Tables.Add(BusObjects.Archiving.GetClosedM onths());
dataSet.Tables[0].Columns.Add("MonthDate");
foreach(DataRow row in dataSet.Tables[0].Rows)
{
row["MonthDate"] = Enum.GetName(typeof(MonthShort), row["Monthx"])
+ " " + row["Yearx"].ToString();
}

listBoxDates.DataSource = dataSet.Tables[0];
listBoxDates.DataTextField = "MonthDate";

listBoxDates.DataBind();
Where 'MonthShort' is a simple enumeration in which Jan = 1, Feb = 2, etc.

Anyhow, this code works fine but I'm wondering if there isn't a simpler way
to do it?
I also have a second question: Is there a way to change the alignment of the
contents of the ListBox to be something other than left-aligned?

--
Robert W.
Vancouver, BC
www.mwtech.com

May 8 '06 #1
3 2815

Look at the ListItem, and add them individually.

ListItem li = new ListItem("--Select--" , 0);
ddlMyDropDown.Items.Add (li);

li = new ListItem("hello");
ddlMyDropDown.Items.Add(li);

There are 3 constructors for ListItem, pick the best one for your needs.

(I'm going by memory above, but it should be close enough)

That's a suggestion if you want to modify the items text ... as it comes
from the database.
(aka, this is a "presentation" change, so looping thru the DataSet items to
alter the display is acceptable)

...

go to the aspx page, and the html ... and see if there is a "align" property
for the listbox. I don't think there is.


"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I have a SQL Server table called "ClosedMonths" that contains two pertinent fields:

Yearx Monthx
2000 1
2000 2
2000 3
I want to simply query this table and populate a WebControls listbox with
the contents, though in this format:

Jan 2000
Feb 2000
Mar 2000
So I wrote the following code:

DataSet dataSet = new DataSet();
dataSet.Tables.Add(BusObjects.Archiving.GetClosedM onths());
dataSet.Tables[0].Columns.Add("MonthDate");
foreach(DataRow row in dataSet.Tables[0].Rows)
{
row["MonthDate"] = Enum.GetName(typeof(MonthShort), row["Monthx"]) + " " + row["Yearx"].ToString();
}

listBoxDates.DataSource = dataSet.Tables[0];
listBoxDates.DataTextField = "MonthDate";

listBoxDates.DataBind();
Where 'MonthShort' is a simple enumeration in which Jan = 1, Feb = 2, etc.

Anyhow, this code works fine but I'm wondering if there isn't a simpler way to do it?
I also have a second question: Is there a way to change the alignment of the contents of the ListBox to be something other than left-aligned?

--
Robert W.
Vancouver, BC
www.mwtech.com

May 8 '06 #2
Dear Sloan,

Thanks for your feedback. But I was actually hoping that some SQL Server
whiz might know a much more streamlined way to 'create' the required data
table "on the fly".
--
Robert W.
Vancouver, BC
www.mwtech.com

"sloan" wrote:

Look at the ListItem, and add them individually.

ListItem li = new ListItem("--Select--" , 0);
ddlMyDropDown.Items.Add (li);

li = new ListItem("hello");
ddlMyDropDown.Items.Add(li);

There are 3 constructors for ListItem, pick the best one for your needs.

(I'm going by memory above, but it should be close enough)

That's a suggestion if you want to modify the items text ... as it comes
from the database.
(aka, this is a "presentation" change, so looping thru the DataSet items to
alter the display is acceptable)

...

go to the aspx page, and the html ... and see if there is a "align" property
for the listbox. I don't think there is.


"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I have a SQL Server table called "ClosedMonths" that contains two

pertinent
fields:

Yearx Monthx
2000 1
2000 2
2000 3
I want to simply query this table and populate a WebControls listbox with
the contents, though in this format:

Jan 2000
Feb 2000
Mar 2000
So I wrote the following code:

DataSet dataSet = new DataSet();
dataSet.Tables.Add(BusObjects.Archiving.GetClosedM onths());
dataSet.Tables[0].Columns.Add("MonthDate");
foreach(DataRow row in dataSet.Tables[0].Rows)
{
row["MonthDate"] = Enum.GetName(typeof(MonthShort),

row["Monthx"])
+ " " + row["Yearx"].ToString();
}

listBoxDates.DataSource = dataSet.Tables[0];
listBoxDates.DataTextField = "MonthDate";

listBoxDates.DataBind();
Where 'MonthShort' is a simple enumeration in which Jan = 1, Feb = 2, etc.

Anyhow, this code works fine but I'm wondering if there isn't a simpler

way
to do it?
I also have a second question: Is there a way to change the alignment of

the
contents of the ListBox to be something other than left-aligned?

--
Robert W.
Vancouver, BC
www.mwtech.com


May 9 '06 #3
the typical sql way would be to create a domain table Monthxx, MonthName and
join to it, or use a case statement, or user defined function if done option

use case:

select case Monthx
when 1 then 'Jan'
when 2 then 'Feb'
when 3 then 'Mar'
when 4 then 'Apr'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'Jul'
when 9 then 'Aug'
when 10 then 'Sep'
when 11 then 'Nov'
when 12 then 'Dec'
end + ' ' + convert(varchar,Yearx) as MonthDate
from ClosedMonths

or calc it:

select substring('JanFebMarAprMayJunJulAugSepOctNovDec',( Monthx-1)* 3 + 1,3)
+ ' ' + convert(varchar,Yearx) as MonthDate
from ClosedMonths

-- bruce (sqlwork.com)

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
Dear Sloan,

Thanks for your feedback. But I was actually hoping that some SQL Server
whiz might know a much more streamlined way to 'create' the required data
table "on the fly".
--
Robert W.
Vancouver, BC
www.mwtech.com

"sloan" wrote:

Look at the ListItem, and add them individually.

ListItem li = new ListItem("--Select--" , 0);
ddlMyDropDown.Items.Add (li);

li = new ListItem("hello");
ddlMyDropDown.Items.Add(li);

There are 3 constructors for ListItem, pick the best one for your needs.

(I'm going by memory above, but it should be close enough)

That's a suggestion if you want to modify the items text ... as it comes
from the database.
(aka, this is a "presentation" change, so looping thru the DataSet items
to
alter the display is acceptable)

...

go to the aspx page, and the html ... and see if there is a "align"
property
for the listbox. I don't think there is.


"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
> I have a SQL Server table called "ClosedMonths" that contains two

pertinent
> fields:
>
> Yearx Monthx
> 2000 1
> 2000 2
> 2000 3
>
>
> I want to simply query this table and populate a WebControls listbox
> with
> the contents, though in this format:
>
> Jan 2000
> Feb 2000
> Mar 2000
>
>
> So I wrote the following code:
>
> DataSet dataSet = new DataSet();
> dataSet.Tables.Add(BusObjects.Archiving.GetClosedM onths());
> dataSet.Tables[0].Columns.Add("MonthDate");
> foreach(DataRow row in dataSet.Tables[0].Rows)
> {
> row["MonthDate"] = Enum.GetName(typeof(MonthShort),

row["Monthx"])
> + " " + row["Yearx"].ToString();
> }
>
> listBoxDates.DataSource = dataSet.Tables[0];
> listBoxDates.DataTextField = "MonthDate";
>
> listBoxDates.DataBind();
>
>
> Where 'MonthShort' is a simple enumeration in which Jan = 1, Feb = 2,
> etc.
>
> Anyhow, this code works fine but I'm wondering if there isn't a simpler

way
> to do it?
>
>
> I also have a second question: Is there a way to change the alignment
> of

the
> contents of the ListBox to be something other than left-aligned?
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>


May 10 '06 #4

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

Similar topics

2
by: James Goodman | last post by:
I have a listbox named sub1 on an asp page. I need to fill this list with values from a table. These are selected based upon the selection of a value/s in another listbox. It was suggested that I...
2
by: Wishing I was skiing mom | last post by:
Newbie to VB .NET. My solution contains an item maintenance screen, one of the fields on the screen is an item status, this field is defined as a listbox. The listbox item property contains a...
1
by: Devin Wood | last post by:
Hi, I have a page with a ListBox on it, and It's also have a some buttons to populate the ListBox by using JavaScript. I have no problem with populate the listbox using JavaScript. But when the...
1
by: Mike | last post by:
How can i populate a list box from a text file? I create a list of files within a text file and I want to populate the list box with that list to allow the users to select the web server to test...
5
by: Arpan | last post by:
In order to populate any server control with data dynamically, is it ALWAYS NECESSARY to either BIND the DataSource to that server control or call the DataBind method of that server control? For...
4
by: Joe Blow | last post by:
Folks, I have read all the posts about databases I could find and I think I need a little more help. I want to take all the items from a field in an access database and use them as list items in...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: Chaihana Joe | last post by:
Hi I'm trying to populate a listbox as a value list in code (access 2003). Unfortunately I'm getting 'runtime error 2176 - the setting for this property is too long' somewhere around the 2000...
2
by: dbuchanan | last post by:
I intend to populate a listBox from the database for later use. (the intent for doing this is irrelevant to this questrion) The list box when hidden is never populated. The list box when visible...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.