Click here to Skip to main content
14,421,696 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I'm developing a c# window application.

I have 5 textboxes,(txtqty1, txtqty2, txtqty3, txtqty4 and txtqty5).

I want to insert the data from the textboxes into a database in the Qty field, but I want to insert data with For loop.

Is it Possible.?

Thanks in Advance
Posted
Updated 1-Dec-10 2:26am
v2
Comments
Dalek Dave 1-Dec-10 7:26am
   
Edited for Readability.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Yes - but it probably isn't a good idea.

You can either:
1) Set up an array of TextBoxes, and assign your boxes to it:
TextBox[] boxes = new TextBox[] { txtqty1,txtqty2,txtqty3,txtqty4,txtqty5};
...
foreach (TextBox t in boxes)
   {
   ... // Add to db
   }

or
2) Use the Form.Controls collection to run through each control, and add each textbox:
foreach (control c in Controls)
   {
   TextBox t = c as Textbox;
   if (t != null)
      {
      ... // Add to db
      }
   }

Either way, it's not a nice way to do things.

In either case, remember to use the db Command object Properties.AddWithValue method to avoid SQL injection attacks.
   
Comments
Dalek Dave 1-Dec-10 7:27am
   
Good Answer.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100