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

ASP.net Looping through a Collection of Dates

I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
....
....
....
....
December 2006
Item
Item
..... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.
Jun 17 '07 #1
7 1748
Mick Walker <Mi*********@privacy.netwrote in news:5djhv5F34s3l4U1
@mid.individual.net:
I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

In your SQL statement, sort the list by date. Then use a datareader (or
dataset) to output the data.
Jun 17 '07 #2
Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor

"Mick Walker" <Mi*********@privacy.netschreef in bericht
news:5d*************@mid.individual.net...
>I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest entry,
and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
...
...
...
...
December 2006
Item
Item
.... and so on.

I have no idea how to achieve this, so if someone could help me that would
be great.

Jun 17 '07 #3
Cor Ligthert [MVP] wrote:
Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor
Thanks for your reply Cor.
However I am still no wiser at the moment. I know what I wish to do, I
am just unsure of how to do it.

I need to loop though my collection,

Process Items Which Are in current Month
Write Heading
Write Items
Process Items Which Are in current Month -1
Write Heading
Write Items
Process Items Which Are in current Month -2
Write Heading
Write Items
Process Items Which Are in current Month -3
Write Heading
Write Items

......
I would like to keep the result set to a max of 6 months.

Any ideas?

Regards
Mick

Jun 17 '07 #4
Mick Walker wrote:
I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
...
...
...
...
December 2006
Item
Item
.... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.
Keep track of the year and month, and check against each item that you
output. When it changes, output a new header.

--
Göran Andersson
_____
http://www.guffa.com
Jun 17 '07 #5

"Mick Walker" <Mi*********@privacy.netwrote in message
news:5d*************@mid.individual.net...
Cor Ligthert [MVP] wrote:
>Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor
Thanks for your reply Cor.
However I am still no wiser at the moment. I know what I wish to do, I am
just unsure of how to do it.

I need to loop though my collection,

Process Items Which Are in current Month
Write Heading
Write Items
Process Items Which Are in current Month -1
Write Heading
Write Items
Process Items Which Are in current Month -2
Write Heading
Write Items
Process Items Which Are in current Month -3
Write Heading
Write Items
What do you mean? Are you talking about looping through a collection class
with a collection of objects.

If that's what you're talking about then you use a Foreach loop

Foreach item as theobject in Collection

if item.Date = current Month then
do something
endif

if item.Date = current Month -1 then
do something
endif
next

Or if you're talking about a SQL recordset, then you use a ADO.Net
Datareader and loop through the resultset.

while dr Not EOF
if dr.Date = current Month then
do something
endif
loop

It's really not clear as to what you're asking.

Jun 18 '07 #6
Mick,

Something like this, executed with a command.executescalar seems proper for
me.
(where here the orders from the Northwind database is used)
Select Top 1 OrderDate From Orders Where OrderDate @previousdate' Order By
OrderDate

Cor

"Mick Walker" <Mi*********@privacy.netschreef in bericht
news:5d*************@mid.individual.net...
Cor Ligthert [MVP] wrote:
>Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor
Thanks for your reply Cor.
However I am still no wiser at the moment. I know what I wish to do, I am
just unsure of how to do it.

I need to loop though my collection,

Process Items Which Are in current Month
Write Heading
Write Items
Process Items Which Are in current Month -1
Write Heading
Write Items
Process Items Which Are in current Month -2
Write Heading
Write Items
Process Items Which Are in current Month -3
Write Heading
Write Items

.....
I would like to keep the result set to a max of 6 months.

Any ideas?

Regards
Mick

Jun 18 '07 #7
On Jun 16, 6:44 pm, Mick Walker <Mick.Wal...@privacy.netwrote:
I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
...
...
...
...
December 2006
Item
Item
.... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.

Try this:

dim tempDate as item(1).date

do while tempDate < item(n).date
...
tempDate = tempDate.addDays(1)
loop

and so on...

Jun 18 '07 #8

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

Similar topics

1
by: Beau | last post by:
Hi all, thanks in advance. Ok, heres the story. What is happening...... -------------------------------- I've got an ASP page that loops. It loops in order to get data in different,...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
3
by: VB Programmer | last post by:
Here's the code: For Each li As ListItem In Me.lstAssignedEmailAddresses.Items If li.Selected = True Then Me.lstAssignedEmailAddresses.Items.Remove(li) End If Next When I remove an item I get...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
1
by: Peter Parker | last post by:
Does anyone know if there is a method or property in php5 that provides the collection size in XML processing? For example, $collection=$doc->getElementsByTagName("someTag"); where one can get...
7
by: Mick Walker | last post by:
I have a collection of objects populated from a SQL Server (sorted descending). Each of these object has a date field. I need a way to loop through these object starting from the newest entry,...
2
by: zags | last post by:
I have a date parameter for a query that I'm using to update a table. I need to run the query several times using a different date each time its run. The dates are pay period end dates that are...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.