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

  • Question

  • Problem

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

    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

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

    {

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

    } 

    How to write for loop to insert data on two fields fromhour and tohour incremented by duration ?


    Tuesday, May 7, 2019 3:36 AM

Answers

  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 1:55 AM
    Moderator

All replies

  • Check an example:

    int first_serial = 1;
    decimal begin = 6m;
    decimal end = 12m;
    decimal step = 1.5m;
    
    int serial = first_serial;
    for( decimal fromhour = begin, tohour = begin + step; tohour <= end; fromhour = tohour, tohour += step, ++serial )
    {
        Console.WriteLine( $"{serial} {fromhour} {tohour}" );
        // TODO: write to database
        // . . .
    }
    


    It writes to console. Add the code that inserts to database.

    Tuesday, May 7, 2019 4:50 AM
  • Hi engahmedbarbary,

    Thank you for posting here.

    For your question, you want to write for loop to insert data on two fields fromhour and tohour incremented by duration.

    I write a simple code, you could have a look.

     private void Button1_Click(object sender, EventArgs e)
            {
                string str = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                SqlConnection sqlConnection = new SqlConnection(str);
                sqlConnection.Open();
                int count =Convert.ToInt32((Convert.ToDecimal(txtTohour.Text) - Convert.ToDecimal(txtFromhour.Text)) / Convert.ToDecimal(txtDuration.Text));
                for (int i = 0; i <count; i++)
                {
                    string Sql = String.Format("insert into training(serial,fromhour, tohour) values('{0}','{1}','{2}')", i+1, Convert.ToDecimal(txtFromhour.Text)+i * Convert.ToDecimal(txtDuration.Text), Convert.ToDecimal(txtFromhour.Text) + (i+1) * Convert.ToDecimal(txtDuration.Text));
                    SqlCommand sqlCommand = new SqlCommand(Sql, sqlConnection);
                    sqlCommand.ExecuteNonQuery();
                }
    
                sqlConnection.Close();
                MessageBox.Show("success");
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 7:31 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 1:55 AM
    Moderator