473,769 Members | 7,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1692
Mick Walker <Mi*********@pr ivacy.netwrote in news:5djhv5F34s 3l4U1
@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*********@pr ivacy.netschree f in bericht
news:5d******** *****@mid.indiv idual.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*********@pr ivacy.netwrote in message
news:5d******** *****@mid.indiv idual.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.execute scalar 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*********@pr ivacy.netschree f in bericht
news:5d******** *****@mid.indiv idual.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...@pr ivacy.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.addDay s(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
3724
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, sequential date ranges. I.E. from 9/1/2000 - 10/1/2000 then 10/1/2000 - 11/1/2000 etc etc etc. It calls SPs using the 2 dates and an integer used for companyid reference.
18
5751
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 not in chronological order. Is there a way to first sort the collection and put the dates in chronological order before creating the value list string? Or, how would I iterate over the collection pulling out the dates in chronological order? ...
45
7475
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) word size was 16 bits same as an integer. Now with a 32 bit CPU I would have expected the long to be faster as it's the same size as the CPU's word size so wouldn't need sawing in half like a magician's assistant to calculate on like an...
16
7238
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
3
2698
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 this error: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at
10
2783
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 like manufacturers, makes, modes, etc. of cars. I have created a generic "datacollection" class and a generic "dataobject" class to represent the table and the rows within that table as a collection of objects with generic properties for...
1
1407
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 the size of the $collection without looping it? Thanks
7
1766
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, and finishing at the last, and output them in the format: June 2007 Item Item Item
2
1241
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 based on a date range that a user enters on a form. For example, a user enters a date range of 01/01/2007 - 03/01/2007 the pay period end dates in that period may be 01/05/07 01/16/07 02/01/07, etc. The dates are stored in a table as follows ...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.