简体   繁体   中英

Data insert dynamically multiple rows one-button click

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:

dropdown-list = 0,1,2,3,4 ; // select any number for insert multiple rows in the database table

[1]textbox= "data" ; // input data
[2]textbox= "data" ; // input data

[button-click]

My 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 < 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();
}

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.

You are closing connection only in catch block. This is what happens. in 1st iteration value is inserted but connection is not closed, in 2nd iteration exception occurred and connection is closed. In 3rd iteration values is inserted again and so on. Here is updated 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 < 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
            {
                i--;
            }
            finally
            {
                con.Close();
            }    
        }
        GridView1.DataBind();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM