14,664,409 members
Rate this:
See more:
Problem

How to write for loop increase from hour by Duration for every iteration ?

I work on windows form application csharp vs2015

i have three textboxes

txt_fromhour 6

txt_tohour 12

txt_Duration 1.5

txt_countduration 4 (calculated as 12-6=6/1.5=4)

I have table name training have 3 fields

serial int

fromhour decimal

tohour decimal

I need to write for loop insert data on three fields as sample below

when i write fromhour 6 and tohour 12 and duration is 1.5 data will inserted as below

```serial    fromhour            tohour

1            6                7.5

2            7.5               9

3            9                10.5

4            10.5              12```

What I have tried:

```for(int i=0;i<=4;i++)

{

insert into training(serial,fromhour,tohour) values(max+1,??,??)

}```
Posted
Updated 6-May-19 20:27pm

Rate this:

## Solution 3

You need to use a variable outside a loop. See:

```//use it for number formatting
System.Globalization.CultureInfo cu = new System.Globalization.CultureInfo("en-us");
int fromhour = 6;
int tohour = 12;
float duration = 1.5F;

int count = (int)((tohour - fromhour) / duration);
//here!
float nextfrom = fromhour;
for(int i=1; i<=count; i++)
{
Console.WriteLine("{0} \t-\t {1}", nextfrom.ToString("0.0", cu), (nextfrom+duration).ToString("0.0", cu));
//insert statement here
nextfrom += duration;
}```

Result:
```6.0   -   7.5
7.5   -   9.0
9.0   -   10.5
10.5   -   12.0```

[EDIT]
I forgot to mention that you have to work with datetime data type instead of numbers to avoid issues in a future.
```DateTime counter = DateTime.Today.AddHours(fromhour);
for(int i=1; i<=count; i++)
{
Console.WriteLine("{0} \t-\t {1}", counter.ToString("HH:mm", cu), counter.AddHours(duration).ToString("HH:mm", cu));
//insert statement here
}```

Result:
```06:00   -   07:30
07:30   -   09:00
09:00   -   10:30
10:30   -   12:00```
v2
Rate this:

## Solution 2

Start by converting yoru three textbox values to numbers: Decimal.TryParse Method (System) | Microsoft Docs[^] will do it, and report errors to the user instead of continuing.

Then create and open your DB connection:
```            using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
...
}```
Inside the open connection, set up a loop: `while` will do, stopping when the `fromHour` is greater than the `toHour`.

Inside the loop, create an an `SQLCommand` object (with a `using` block as above) and pass the two times to the DB as a parameterized query. If necessary, pass the `serial` value each time (though I'd make it an IDENTITY field in the DB, so SQL handles all that) Increment the `fromHour` by the `duration`.

Easy to do - just give it a try manually and you will see what I mean.
Rate this:

## Solution 1

Store your duration as a variable. Set a timer to go off every hour and increment it. If you want a permanent increase from a given time, use DateTime.Now to calculate it on a property.
ahmed_sa 7-May-19 0:37am

can you give me more clear or write psudo code for that please
Christian Graus 7-May-19 0:38am

I can't make it any clearer. Set a timer to increment your value hourly. Store the value you want to use in a variable, set it from that variable.