sql insert doing for loop

Total Post:10

Points:70
C# 
C#
 2716  View(s)
Ratings:
Rate this:

Hi guys,

Server controls: Label, TextBox, Button and GridView.

I need some help regarding my issue.

If a user would like to enter the ff:

Enter Number of Payments: 3    <Button Details> - if pressed     >>> 3 x a user

will input values

Number of Payments - auto-generated value according to number of

payments entered

it will show:

Pay# : 1 
Week: 1
Percent/wk: 50

loop

Pay#: 2
Week: 5
Percent/wk: 30

Pay#: 3
Week: 9
Percent/wk: 20


The time it will reach the third entry it will stop the operation. All those entries will be saved directly on sql server database. The week entry increments by 4 while the condition is true.

I have an idea on how to insert data however, this concern uses an iteration until it satisfies the condition to terminate the insertion.

Any help are highly appreciated. Thanks.
  1. Post:65

    Points:455
    Re: Server-control looping

    Hi,

    Could you please explain your question in more details.

    Thanks.

  1. Post:48

    Points:336
    Re: Server-control looping

    Hi Sheen,

    You can try following code format to complete your task.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();   //create a datatable object.
                //create some column in datatable object as much you required.
                dt.Columns.Add("pay");   
                dt.Columns.Add("week");
                dt.Columns.Add("percentweek");
                int maxLoop = 3;   //This value is enterd by user
                for (int i = 0; i < maxLoop; i++)
                {
                    DataRow dr = dt.NewRow();     //create row in DataTable object.
                    dr[0] = (i + 1).ToString();
                    dr[1] = "4";    //This value is enterd by user
                    dr[2] = "20";   //This value also enterd by user.
                    dt.Rows.Add(dr);
                }
                dataGridView1.DataSource = dt.DefaultView;   //Bindrecord in datagridview.
            }

     

    Maybe this is helpful for you.

     

    Please mark it as a answer if it is that which you want.

     

    Thanks,

      Modified On Mar-29-2018 04:48:45 AM
  1. Post:10

    Points:70
    Re: Server-control looping

    Hi James,


    Thanks for your thoughts however I will give this a try and if this still doesn't work then I will revert back.
    It's been a long day!


    Cheers!

      Modified On Mar-29-2018 04:48:57 AM
  1. Post:10

    Points:70
    Re: Server-control looping

    Hi James,

    I need some help which is very similar to the posted thread on how to loop upon insertion of record.
    I bet your example is primarily assigning a value to a variable.
    If a user let say enters value of 4, an insertion of values will be done 4 times until the execution will be terminated. There will be a counter value displayed on the form on how many times a user inserts those values.

    I have re-phrased my thread above. If there are sample snippets for this that would be great.

    Thanks.

      Modified On Mar-29-2018 04:49:09 AM
  1. Post:48

    Points:336
    Re: sql insert doing for loop

    Hi Sheen,
    As you stated previously and starting up, I am little bit confuse about the question and requirements,
    If you will explain your question in more detail, what is exactly your requirement then that is more useful for me to reply,

    Thanks,

  1. Post:10

    Points:70
    Re: sql insert doing for loop

    Post is removed by the User.

  1. Post:10

    Points:70
    Re: sql insert doing for loop

    Hi James,

    As per illustration stated above, when a user entered a counter value of 3 it will loop 3 times. By the time it will loop 3 times it will also insert the record 3 times.
    Example:
    Number of payments: 3 [button]
    Pay# : 1
    Week: 1

    Percent/wk: 50
    [press Save button] it will loop to perform Pay# 2

    Pay#: 2
    Week: 5
    Percent/wk: 30
    [press Save button] it will loop to perform Pay# 3

    Pay#: 3
    Week: 9
    Percent/wk: 20
    [press Save button] this will terminate the insert command

    Hope this statement is clear. Thanks.

      Modified On Mar-29-2018 04:49:27 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!