title: Data insert dynamically multiple rows one-button click
body: <p>I want to insert data by clicking button one time where there have a dropdown list selected number and two textboxes. I select dropdown list numbers [ex. 2] and give input data in two textboxes then click insert button one time. data save in the database table multiple rows, how many numbers select from dropdown list. for example:</p>
  
  <p>dropdown-list = 0,1,2,3,4 ; // select any number for insert multiple rows in the database table</p>
  
  <p>[1]textbox= "data" ; // input data<br>
  [2]textbox= "data" ; // input data</p>
  
  <p>[button-click]</p>
  
  <p>My code:</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
      con = new SqlConnection();
      con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
      string value = DropDownList4.SelectedValue.ToString();  // Get the dropdown value 
      int count = 0;
      int.TryParse(value, out count);  // cast the value to integer 
      for (int i = 0; i &lt; count; i++)  // iterate it for the N times 
      {
          SqlCommand insert = new SqlCommand("insert into Test(Name, Username) values(@Name, @Username)", con);
          insert.Parameters.AddWithValue("@Name", TextBox1.Text);
          insert.Parameters.AddWithValue("@Username", TextBox2.Text);
          try
          {
              con.Open();
              insert.ExecuteNonQuery();
          }
          catch
          {
              con.Close();
          }
      }
      GridView1.DataBind();
  }
  </code></pre>
  
  <p>This code can't insert data correctly in the database. when I select dropdwn-list value 3, row inserted 2 times. when select 5, inserted 3 times.</p>