Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,

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

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


2.form2
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));
Controls.Add(tb);

}

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

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

Solution 1

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 += ")";
   
Comments
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?
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
v2
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100