Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got references for adding dynamic textbox values and storing all the textbox values into the database on single submit button using Asp.net.But now I need the same task to be done in windows form.Can some one help me how to insert all the dynamic textbox values on single click of submit button.(For example:If I add some five rows of text boxes all the five rows should be inserted into database).I am using Windows Application Form,SQl Server and Linq to SQL class.
Note:I am able to insert one row at a time.But I need multiple rows to be inserted into database.

What I have tried:

C#
public partial class Form1 : Form
    {
        List<textbox> textBoxes;
        TextBox tb1;
        TextBox tb2;
        TextBox tb3;
        TextBox tb4;
        ComboBox cm;
        int lbltext = 1;
        int A = 1;
        int i = 0;
        int j = 0;
        public Form1()
        {
            InitializeComponent();
            textBoxes = new List<textbox>();
        }
        //Add Button for adding Dynamic TextBoxes
        private void Button1_Click(object sender, EventArgs e)
        {
            Show(); 
            if (A > 12)
            {
                MessageBox.Show("Cannot insert More than 12 records in a page");
            }
            A += 1;
            lbltext += 1;
        }
        private new void Show()
        {
            i++;
            tb1 = new TextBox()
            {
                Text = (lbltext).ToString() + ".",
                Height = 22,
                Width = 17,
                Left = 9,
                Top = 140 + A * 36,
             // Parent = FormWindowState.Normal,
                Name = "textbox" + i
            };
            i++;
            tb2 = new TextBox
            {
                Height = 22,
                Width = 60,
                Left = 43,
                Top = 140 + A * 36,
                Name ="textbox"+i
            };
            i++;
            tb3 = new TextBox {
                Height = 22,
                Width = 64,
                Left = 122,
                Top = 140 + A * 36,
                Name="textbox"+i
            };
            i++;
            tb4 = new TextBox {
            Height = 22,
            Width = 80,
            Left = 220,
            Top = 140 + A * 36,
            Name ="textbox"+i
            };
            j++;
            cm = new ComboBox();
            cm.Text = "-NONE-";
            cm.Items.Add("-PRESENT-");
            cm.Items.Add("-LEAVE-");
            cm.Items.Add("-HOLIDAY-");
            cm.Left = 358;
            cm.Width = 92;
            cm.Height = 22;
            cm.Name = "ComBoBox" + j;
            cm.Top = 140 + A * 36;
            textBoxes.Add(tb1);
            textBoxes.Add(tb2);
            textBoxes.Add(tb3);
            textBoxes.Add(tb4);
            this.Controls.Add(tb1);
            this.Controls.Add(tb2);
            this.Controls.Add(tb3);
            this.Controls.Add(tb4);
            this.Controls.Add(cm);
        }
       //Submitting values to Database
        private void Button2_Click(object sender, EventArgs e)
        { 
            ExampleDataContext dc = new ExampleDataContext();
            TextBox3 tb = new TextBox3();
            TextBox4 dtb4 = new TextBox4();
            TextBox5 dtb5 = new TextBox5();
            TextBox6 dtb6 = new TextBox6();
            ComboBox2 dcm = new ComboBox2();
            Lable lable = new Lable();
            try
            {   
                    lable.textvalue = lbltext.ToString();
                    //For Storing S.No
                    tb.textvalue = lbltext - 1;
                    tb.HeightValue = tb1.Height;
                    tb.WidthValue = tb1.Width;
                    tb.LeftValue = tb1.Left;
                    tb.TopValue = tb1.Top;
                    tb.name = tb1.Name;
                    select tb.textvalue;
                    dc.TextBox3s.InsertOnSubmit(tb);
                    dc.SubmitChanges();
                    //For Storing Id
                    dtb4.textvalue = tb2.Text;
                    dtb4.HeightValue = tb2.Height;
                    dtb4.WidthValue = tb2.Width;
                    dtb4.LeftValue = tb2.Left;
                    dtb4.TopValue = tb2.Top;
                    dtb4.name = tb2.Name;
                    dc.TextBox4s.InsertOnSubmit(dtb4);
                    dc.SubmitChanges();
                    //For Storing Name
                    dtb5.textvalue = tb3.Text;
                    dtb5.HeightValue = tb3.Height;
                    dtb5.WidthValue = tb3.Width;
                    dtb5.LeftValue = tb3.Left;
                    dtb5.TopValue = tb3.Top;
                    dtb5.name = tb3.Name;
                    dc.TextBox5s.InsertOnSubmit(dtb5);
                    dc.SubmitChanges();
                    //For Storing Designation
                    dtb6.textvalue = tb4.Text;
                    dtb6.HeightValue = tb4.Height;
                    dtb6.WidthValue = tb4.Width;
                    dtb6.LeftValue = tb4.Left;
                    dtb6.TopValue = tb4.Top;
                    dtb6.name = tb4.Name;
                    dc.TextBox6s.InsertOnSubmit(dtb6);
                    dc.SubmitChanges();
                    //For Storing Attendence ComboBox
                    dcm.LeftValue = cm.Left;
                    dcm.HeightValue = cm.Height;
                    dcm.TopValue = cm.Top;
                    dcm.WidthValue = cm.Width;
                    dcm.textvalue = cm.Text;
                    dcm.name = cm.Name;
                    dc.ComboBox2s.InsertOnSubmit(dcm);
                    dc.SubmitChanges();
                    MessageBox.Show("Record inserted successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Record already exists" + ex.Message);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {   //For maintaining the names of dynamic textboxes when the form got loaded 
            //for the second time
            this.WindowState = Properties.Settings.Default.F1State;
          
            }
        }
Posted
Updated 28-Aug-19 23:54pm
v2
Comments
Afzaal Ahmad Zeeshan 29-Aug-19 5:10am    
I would recommend using data binding in these cases, they would simplify the structure a lot. :-)

1 solution

You're on the right track and your code needs small improvement:

C#
private void Button2_Click(object sender, EventArgs e)
{ 
//textboxes creation here

//later
EntityContext ec = new EntityContext()
{
    TextBox3s = tb,
    TextBox4s = dtb4,
    TextBox5s = dtb5,
    TextBox6s = dtb6,
    ComboBox2s = cm
};

ExampleDataContext dc = new ExampleDataContext()
dc.InsertOnSubmit(ec);
dc.SubmitChanges();
}


Note: EntityContext is a class which field names and types correspond to your table's fields.
For further details, please see: Table<TEntity>.InsertOnSubmit(TEntity) Method (System.Data.Linq) | Microsoft Docs[^]

Note: Using field names equal to control's names is very strange...
 
Share this answer
 
Comments
Jayanth Prince 29-Aug-19 6:47am    
@Maciej Los,
Thanks a lot
Note:Actually I am a beginner,trying to save those dynamic text boxes into database and when we open the form from the second time all the textboxes should be stable at the same positions along with the properties.So after saving them I will try to retreive back to the form.
I am getting an error when i am trying to create object for EntityContext."The type or namespace name 'EntityContext' could not be found (are you missing a using directive or an assembly reference?)"

Can you help me
Maciej Los 29-Aug-19 7:00am    
As i mentioned: "EntityContext is a class which field names and types correspond to your table's fields". This means taht you have to create it! Follow the link i provided.
Jayanth Prince 29-Aug-19 7:15am    
GotIt
Maciej Los 29-Aug-19 7:40am    
Super!
Cheers!
Maciej

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