title: Constructing a insert query based on a LINQ query
body: <p>I am trying to build a sql insert query based on the number of filled out textboxes returned from a <code>LINQ</code> query. Basically, the start of the textboxes start at the tab index number 13 and ends at the tab index number 33 and adds the non empty textboxes to a <code>keyvaluepair</code> list. The issue that I am confused about is how to add the filled out textboxes' values to named parameters inside the insert query without having a error of <code>number of query values and destination fields are not the same</code>. Here is the code I have in place:</p>
  
  <pre><code> // use LINQ to fetch all the children textboxes based on the ones that are not empty
   System.Collections.Generic.Dictionary&lt;string, string&gt; dictionary = tabCtrl1.TabPages["tabPage1"].Controls.OfType&lt;TextBox&gt;()
      .Where(t =&gt; t.TabIndex &gt;= startTabIndex &amp;&amp; t.TabIndex &lt;= endTabIndex &amp;&amp; !string.IsNullOrWhiteSpace(t.Text))
      .Select(x =&gt; new System.Collections.Generic.KeyValuePair&lt;string, string&gt;(x.Name, x.Text))
      .ToDictionary(z =&gt; z.Key, z =&gt; z.Value);
  
   // loop through all the children textboxes
   // and assign them to the list members.childTextBoxes
   foreach (System.Collections.Generic.KeyValuePair&lt;string, string&gt; kvp in dictionary)
   {
       members.childTextBoxes.Add(new System.Collections.Generic.KeyValuePair&lt;string, string&gt;($"{kvp.Key}", $"{kvp.Value}"));
   }
  </code></pre>
  
  <p>and then constructing of the insert query:</p>
  
  <pre><code> for (int i = 0; i &lt; members.childTextBoxes.Count; i++)
   {
       using (members.DBCommand = new System.Data.OleDb.OleDbCommand("INSERT INTO children (pid, childName, birthday, childEmail)" +
           "VALUES (" + lastInsertId + ",  @"  + members.childTextBoxes[i].Key + ")", members.DBConnection))
       {
           members.DBCommand.Parameters.AddWithValue("@" + members.childTextBoxes[i].Key, members.childTextBoxes[i].Value);
       }
           // error occurs here. i'm assuming its to do 
           if (members.DBCommand.ExecuteNonQuery() &gt; 0)
           {
                MessageBox.Show("Records inserted", "QBC", MessageBoxButtons.OK);
           }
      } 
  }
  </code></pre>
  
  <p>I hope this is enough information that describes the problem I am confused about. </p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks!</p>
  
  <p>I can try to add more information if it helps make my question more clear. </p>
  
  <p>update-</p>
  
  <p>I went ahead and tried to use .Add instead of .AddWithValue but unfortunately that kept giving me an insert into query syntax error.</p>
  
  <p>Here is the updated code for the insert query builder:</p>
  
  <pre><code> string fieldList = $"{string.Join(",", members.childTextBoxes.Select(tb =&gt; mapToDatabase[tb.Key]))}";
   string valueList = $"{string.Join(",", members.childTextBoxes.Select(tb =&gt; "?"))}";
  
   string insertQuery = $"INSERT INTO children {fieldList} VALUES {valueList}";
  
   using (members.DBCommand = new System.Data.OleDb.OleDbCommand(insertQuery, members.DBConnection))
   { 
       foreach (var field in members.childTextBoxes)
       {
           members.DBCommand.Parameters.Add("@" + field.Key, OleDbType.LongVarChar).Value = field.Value;
       }
  
  
       if (members.DBCommand.ExecuteNonQuery() &gt; 0) // error occurs here
       {
           MessageBox.Show("Records inserted", "QBC", MessageBoxButtons.OK);
       }
   }
  </code></pre>