-1

I have 6 Labels and 6 Objects (1 Datepicker, 2 comboboxes and 3 textboxes). The Labels names are columnnames from a mysql database. And the content of the objects are the values for this.

If I click "Save" i want to save the values into the database.

What I found (but doesn't work because I have not the same amount of textboxes like the labels):

string query = "INSERT INTO "+ privateCustomerTablename +" (";
                for (int i = 0; i < textboxes.Count; i++)
                {
                    var column = labels[i];
                    if (i < textboxes.Count - 1)
                        query += column.Name + ",";
                    else
                        query += column.Name;
                }
                query += ") VALUES (";
                for (int i = 0; i < textboxes.Count; i++)
                {
                    var value = textboxes[i];
                    if (i < textboxes.Count - 1)
                        query += "'" + value.Text + "',";
                    else
                        query += "'" + value.Text + "')";
                }

So I though to store all Children of a Stackpanel (I have 2 Stackpanels, 1 for Labels and 1 for the objects) to change textboxes.Count in the objectlist count.

But How I can store the Children of a Stackpanel with different objects in it in a list to do that?

O Jean
  • 105
  • 9
  • You shouldn't be constructing a SQL query by just concatenating strings like this; you should be using a parameterized query. – Servy Mar 20 '17 at 14:42
  • I have bad experience with parameters. The Parameters doesnt work often. – O Jean Mar 20 '17 at 14:43
  • The alternative is [much, much worse](https://xkcd.com/327/). Take the time to learn how to use them properly. It's simply a necessity, not just something nice to do. – Servy Mar 20 '17 at 14:45

1 Answers1

1

But How I can store the Children of a Stackpanel with different objects in it in a list to do that?

You could store all children in a single List<UIElement>:

List<UIElement> allElements = stackPanel1.Children.OfType<UIElement>().ToList();

Or you could store elements of a specific in a specialized list, e.g.:

List<TextBox> allTextBoxesInTheStackPanel = stackPanel1.Children.OfType<TextBox>().ToList();

Does this answer your question?

As for the SQL part you should always prefer using parameters:

Why do we always prefer using parameters in SQL statements?

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88