locked
Save row by row data of TableLayoutPanel in sql RRS feed

  • Question

  • User2003675111 posted

    Hi all,

    I have created dynamic control in TableLayoutPanel and there are multiple rows in TableLayoutPanel.

    Now i want to save one by one row data of these control like textbox, combobox into database which are generated dynamic .

    Please help on this.

    Thursday, June 4, 2020 12:40 PM

Answers

  • User2003675111 posted

    Final Code for the same requirement.

    for (int i = 0; i <= this.TableLayoutPanel1.RowCount; i++)
    {
    string[] datatxt = new string[this.TableLayoutPanel1.ColumnCount];
    for (int j = 0; j <= this.TableLayoutPanel1.ColumnCount; j++)
    {
    Control c = this.TableLayoutPanel1.GetControlFromPosition(j, i);

    if (c != null)
    {
    Console.WriteLine(c.Text);
    string value = c.Text;
    if ((value != "+") && (value != "-"))
    {
    datatxt[j] += value;
    }
    }
    }
    if (datatxt[0] != null)
    {
    string strquery = "insert into tblMaster(a,b,c,d) values('" + datatxt[0] + "','" + datatxt[1] + "','" + datatxt[2] + "','" + datatxt[3] + "')";
    con.Open();
    cmd = new SqlCommand(strquery, con);
    cmd.ExecuteNonQuery();
    con.Close();
    }
    datatxt = null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 5:25 AM

All replies

  • User475983607 posted

    What do you need help with what exactly?  Writing SQL?  Are you having trouble designing dynamic inputs? 

    I want to mention, Web Forms come with data bound controls which are APIs specifically designed to render dynamic controls and handle updates through a standard event handler interface.

    Anyway, if you share your code and explain the problem you are trying to solve then community will be in a better position to provide assistance.

    Thursday, June 4, 2020 12:49 PM
  • User2003675111 posted

    Just i need help to get controls value  before inserting data into sql.

    How to get row by row data from TableLayoutPanel?

    Excel           OpenExcel            1           2

    Browser      KillUrl                    3          4

    string[] datatxt = new string[this.TableLayoutPanel1.Controls.Count];
    string[] datacb = new string[3];
    for (int r = 0; r < TableLayoutPanel1.RowCount; r++)
    {
    for (int i = 0; i < this.TableLayoutPanel1.Controls.Count; i++)
    {
    if (this.TableLayoutPanel1.Controls[i] is TextBox)
    {
    TextBox txtserial = (TextBox)this.TableLayoutPanel1.Controls[i];
    string value = txtserial.Text;
    datatxt[i] += value;
    value = "";
    }
    if (this.TableLayoutPanel1.Controls[i] is ComboBox)
    {
    ComboBox cbserial = (ComboBox)this.TableLayoutPanel1.Controls[i];
    string value = cbserial.Text;
    datacb[i] += value;
    value = "";
    }
    }

    Thursday, June 4, 2020 1:53 PM
  • User475983607 posted

    When you decided to create dynamic controls then you lost the standard state management feature in .NET Web Forms.  That makes sense, right, because there no record of the dynamic controls in the page markup or designer file.  The framework has no idea the controls exist when the page loads.  It is up to you to write code that adds the expected dynamic controls early in the page life cycle.  This will allow the framework to bind the post values to the controls.  You did not post state management code so I have no idea if you you've written the code or not.  I think it is important to understand that dynamic controls in Web Forms is a very mature subject and a quick Google search will turn up many tutorials and blogs.  Even the FAQs on this forum covers this programming pattern.

    With that being said, I recommend using standard data bound controls rather than dynamic controls.  

    Thursday, June 4, 2020 3:08 PM
  • User2003675111 posted

    have we any TableLayoutPanel control in asp.net?

    I am looking code for window app using c#

    Thursday, June 4, 2020 3:45 PM
  • User475983607 posted

    have we any TableLayoutPanel control in asp.net?

    I am looking code for window app using c#

    You are in the wrong forum.  This is an ASP.NET support forum for building web site.  You need a Windows App support forum.

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms

    Thursday, June 4, 2020 4:48 PM
  • User2003675111 posted

    Thanks for support

    Friday, June 5, 2020 5:15 AM
  • User288213138 posted

    Hi Neeraj Yadav,

    You can try below code, you can use the TableLayoutPanel. GetControlFromPosition method returns the child control occupying the specified position.

       for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++)
                {
                    Control c = this.tableLayoutPanel1.GetControlFromPosition(i, j);
                    Console.Write(((TextBox)c).Text);
                }
                Console.WriteLine();
            }

    Best regards,

    Sam

    Friday, June 5, 2020 6:10 AM
  • User2003675111 posted

    Final Code for the same requirement.

    for (int i = 0; i <= this.TableLayoutPanel1.RowCount; i++)
    {
    string[] datatxt = new string[this.TableLayoutPanel1.ColumnCount];
    for (int j = 0; j <= this.TableLayoutPanel1.ColumnCount; j++)
    {
    Control c = this.TableLayoutPanel1.GetControlFromPosition(j, i);

    if (c != null)
    {
    Console.WriteLine(c.Text);
    string value = c.Text;
    if ((value != "+") && (value != "-"))
    {
    datatxt[j] += value;
    }
    }
    }
    if (datatxt[0] != null)
    {
    string strquery = "insert into tblMaster(a,b,c,d) values('" + datatxt[0] + "','" + datatxt[1] + "','" + datatxt[2] + "','" + datatxt[3] + "')";
    con.Open();
    cmd = new SqlCommand(strquery, con);
    cmd.ExecuteNonQuery();
    con.Close();
    }
    datatxt = null;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 5:25 AM
  • User288213138 posted

    Hi Neeraj Yadav,

    Final Code for the same requirement.

    Do you solve the problem? If not, please explain your problem in detail.

    Best regards,

    Sam

    Monday, June 8, 2020 9:42 AM