locked
inserting using for loop

    Question

  • sir

    i have four text boxes and four radio buttons.

    want to enter values to all text boxes and select any one radio button and i want save the textbox value and particular radio button checked value.

    now i am using seperate insert Query for all text boxes and i want to use for loop.

    please give complete code 

    Saturday, June 23, 2012 6:00 AM

Answers

  • Hello,

    Considering it that each TextBox and each RadioButton won't have the same names,so theorily speaking you CANNOT use foreach to different kinds of names for these controls;However you can do the trick like——

    1)Use something like List<TextBox> and List<RadioButton> to contain these controls,and then dynamically generate these controls and put them onto the WinForm。

    2)Then use foreach to loop TextBoxes or RadioButtons to fetch these values and dynamically combine a whole Sql insert statement and do Execution with the help of SqlCommand……

    Reguards!


       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Saturday, June 23, 2012 6:07 AM
  • Like he said, here is an example:

        public partial class Form1 : Form
        {
            TextBox[] tbs;
            RadioButton[] rbs;
            public Form1()
            {
                InitializeComponent();
                tbs = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
                rbs = new RadioButton[] { radioButton1, radioButton2, radioButton3, radioButton4 };
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < rbs.Length; i++)
                {
                    if (rbs[i].Checked)
                    {
                        string text = tbs[i].Text;
                        //do the code in here (text value is from the coresponding checked radionButton)
                        //do the Insert query here...
                        //and on the end:
                        break;
                    }
                }
            }
        }


    Mitja

    Saturday, June 23, 2012 6:49 AM

All replies

  • Hello,

    Considering it that each TextBox and each RadioButton won't have the same names,so theorily speaking you CANNOT use foreach to different kinds of names for these controls;However you can do the trick like——

    1)Use something like List<TextBox> and List<RadioButton> to contain these controls,and then dynamically generate these controls and put them onto the WinForm。

    2)Then use foreach to loop TextBoxes or RadioButtons to fetch these values and dynamically combine a whole Sql insert statement and do Execution with the help of SqlCommand……

    Reguards!


       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Saturday, June 23, 2012 6:07 AM
  • Like he said, here is an example:

        public partial class Form1 : Form
        {
            TextBox[] tbs;
            RadioButton[] rbs;
            public Form1()
            {
                InitializeComponent();
                tbs = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
                rbs = new RadioButton[] { radioButton1, radioButton2, radioButton3, radioButton4 };
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < rbs.Length; i++)
                {
                    if (rbs[i].Checked)
                    {
                        string text = tbs[i].Text;
                        //do the code in here (text value is from the coresponding checked radionButton)
                        //do the Insert query here...
                        //and on the end:
                        break;
                    }
                }
            }
        }


    Mitja

    Saturday, June 23, 2012 6:49 AM
  • Have you resolved your problem?
    Tuesday, June 26, 2012 4:37 PM