title: create a dynamic query using cmd.parameter c#
body: <p>I know there are many questions related to create a dynamic query but no one can help me.
  I am trying to create a dynamic <code>cmd.Parameters.AddWithValue</code> with the help of <code>foreach</code> loop but I can't find all the values that I pass in <code>foeach</code>. I know I am writing <strong>var values = " "</strong> that's way the values give me <code>null</code> value </p>
  
  <p>I am providing my code may that help you to solve an error   </p>
  
  <pre><code>  public void insert_Para(System.Web.UI.HtmlControls.HtmlGenericControl ControlName, String TableName)
      {
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString()))
          {
              var values = "";
              SqlCommand cmd = new SqlCommand("insert into "+ TableName +" values(" + values + ")", con);
              cmd.CommandType = System.Data.CommandType.StoredProcedure;
              foreach (var item in ControlName.Controls)
              {
                  if (item is TextBox)
                  {
                      cmd.Parameters.AddWithValue("@" + ((TextBox)item).ID, ((TextBox)item).Text);
                      values += "@" + ((TextBox)item).ID + ",";
                  }
              }
  
              if (con.State == ConnectionState.Closed)
                  con.Open();
              cmd.ExecuteNonQuery();
          }
      }
  </code></pre>