Click here to Skip to main content
14,733,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i have a form which contains numericupdown control and a button,in running i give a number and click a button. then it will open form2 ,there it will create textboxes the number i give in numericupdown control.

my question is based on the number i have to create a table and columns and after insert the data which i have given in the textbox.i have the following classes

private void button1_Click(object sender, EventArgs e)
            Form2 f = new Form2();
            global.count = Convert.ToInt32(numericUpDown1.Value.ToString());

private void Form2_Load(object sender, EventArgs e)
for (int i = 0; i <global.count;> {
TextBox tb = new TextBox();
tb.Name = "TextBOX" + i;
tb.Size = new Size(100, 25);
tb.Location = new Point(25, 25 + (i * 30));


DAL dal = new DAL();//dal class
string create = "create table MYTABLE()";//how to create columns dynamically

Use below code to generate query dynamically and execute it on database using your method:
string create = "CREATE TABLE MYTABLE (";
for (int i = 1; i <= count; i++)
    create += "column" + i.ToString() + " INT";

    if (i != count)
        create += ",";
create += ")";
prasadhp 6-Aug-13 8:19am
thanks a lot, plz send me insert c

md also
Gautam Raithatha 6-Aug-13 8:43am
what is your database datatype for corresponding columns of these textboxes?
Build insert query as below:
string insert = "INSERT INTO MYTABLE (";
string values = string.Empty;

for (int i = 1; i <= count; i++)
    insert += "column" + i.ToString();
    TextBox txtbox = (TextBox)this.Controls.Find("TextBOX" + i.ToString(), true)[0];    //set textbox id to search as per your form
    values += "'" + txtbox.Text.Trim() + "'";   //omit single quotes if datatype is integer i.e. values += txtbox.Text.Trim();

    if (i != count)
        insert += ",";
        values += ",";
insert += ") VALUES (" + values + ")";

//insert code here
prasadhp 7-Aug-13 2:13am
Index was outside the bounds of the array.get in below line
(TextBox txtbox = (TextBox)this.Controls.Find("TextBOX" + i.ToString(), true)[0];)
Gautam Raithatha 7-Aug-13 2:51am
Your text box ids start from 0 i.e. TextBOX0, TextBOX1, etc.
And in above code it finds from 1 i.e. TextBOX1, TextBOX2 etc.
So the last textbox will not be found in the loop (also from first iteration it will take value from wrong text box). Replace that line with below:

TextBox txtbox = (TextBox)this.Controls.Find("TextBOX" + (i - 1).ToString(), true)[0];

(I hope as a developer you must have solved such an issue.)

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900