Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Every one

I have a DataTable of 2100 records or more. I want to run a loop of 500 data.

So if i have a 1000 data than my loop will be of 2 or i have a 1500 data than my loop will be of 3

i have done this much of code but does't work

In below code dtRecepient is DataTable
for (int recepient = 0; recepient < dtRecepients.Rows.Count; recepient++)
{

}
Posted
Updated 16-Jul-14 22:51pm
v2
Comments
ArunRajendra 17-Jul-14 4:51am    
So?
Rahul JR 17-Jul-14 5:12am    
If i have 550 data in DataTable than my for will run 550 times but after every 500 data i have to assign a some different value to data

If you mean how many loops of 500 you have to do on, say, n records then here you are:
C#
int loops = n / 500;
int spare = n % 500;


spare gives you the number of the remaining records if n is not exactly divisible by 500 (e.g. n=2100 => spare=100).
 
Share this answer
 
C#
int total = dtRecepients.Rows.Count;
int iterations = total/500;

for (int recepient = 1; recepient <= iterations; recepient++)
{

}
 
Share this answer
 
use this approach to get the desired output,

int startIndex, endIndex, totalCount;
totalCount = dtRecepients.Rows.Count;
startIndex = 0; endIndex = 500;
if (totalCount < endIndex)
{
for(startIndex; startIndex }
else
{
DataTable dtDataTemp = dtRecepients.Clone();
while (endIndex < totalCount)
{
dtDataTemp.Rows.Clear();
if (endIndex > totalCount)
{ endIndex = totalCount; }
for (int stIndex = startIndex; stIndex < endIndex; stIndex++)
{
//do your stuff
}
startIndex = endIndex;
endIndex += 500;
}
if (startIndex < totalCount && totalCount < endIndex)
{
dtDataTemp.Rows.Clear();
if (endIndex > totalCount)
{ endIndex = totalCount; }
for (int stIndex = startIndex; stIndex < endIndex; stIndex++)
{
//do your stuff
}
startIndex = endIndex;
endIndex += 500;
}
}
}
else
{ }
 
Share this answer
 
v2
Comments
Rahul JR 17-Jul-14 9:00am    
Thanks sharmAnuj
Sharmanuj 18-Jul-14 1:37am    
Hi Rahul,

thanks for marking the solution.

Regards
Anuj Sharma

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900