How to save to a database using multiple dynamic panels?

sweetSteel 1 Reputation point
2021-10-28T16:44:44.233+00:00

I have a panel which create multiple dynamic textboxes which I save to a database using

foreach (TextBox textBox in pn1TextBoxes.Controls.OfType<TextBox>())
{

     //textBox        save in database.
               

}
Now I added a second dynamic panel pnComments which has the same number of elements as the first (pn1TextBoxes0.
I want to save the controls of the two panels with one sql statement. something like
But I don't know how to run though the panel controls at the same time and get the value.
Help please

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 125.3K Reputation points
    2021-10-28T19:01:19.29+00:00

    Try something like this:

    var textboxes = pn1TextBoxes.Controls.OfType<TextBox>( ).OrderBy( c => c.Top );
    var comments = pnComments.Controls.OfType<TextBox>( ).OrderBy( c => c.Top );
    
    var pairs = textboxes.Zip( comments, ( t, c ) => new { t, c } );
    
    foreach( var pair in pairs )
    {
       TextBox textbox = pair.t;
       TextBox comment = pair.c;
       // . . .
    }
    

    It assumes that the controls are arranged vertically in each panel.

    You can also consider a different design. Define a User Control that includes the textbox, the comment and other controls, and add it dynamically (instead of complex manipulation of separate controls).

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.