Home > DeveloperSection > Forums > sql insert doing for loop
Sheen Buhay

Total Post:10

Points:70
Posted on    October-11-2011 7:23 PM

 C# C# 
Ratings:


 7 Reply(s)
 2071  View(s)
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.


Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    October-12-2011 12:10 AM

Hi,

Could you please explain your question in more details.

Thanks.

James Smith

Total Post:48

Points:336
Posted on    October-12-2011 12:54 AM

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,


Sheen Buhay

Total Post:10

Points:70
Posted on    October-12-2011 4:26 AM

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 Oct-12-2011 04:46:02 AM

Sheen Buhay

Total Post:10

Points:70
Posted on    November-05-2011 3:17 AM

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 Nov-05-2011 03:58:33 AM

James Smith

Total Post:48

Points:336
Posted on    November-07-2011 4:07 AM

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,

Sheen Buhay

Total Post:10

Points:70
Posted on    November-07-2011 6:52 PM

Post is removed by the User.

Sheen Buhay

Total Post:10

Points:70
Posted on    November-07-2011 6:52 PM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick