简体   繁体   English

数据动态插入多行一键式单击

[英]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. 2],并在两个文本框中输入数据,然后单击一次插入按钮。 data save in the database table multiple rows, how many numbers select from dropdown list. 数据保存在数据库表中的多行中,从下拉列表中选择多少个数字。 for example: 例如:

dropdown-list = 0,1,2,3,4 ; dropdown-list = 0,1,2,3,4; // select any number for insert multiple rows in the database table //选择任何数字以在数据库表中插入多行

[1]textbox= "data" ; [1] textbox =“数据”; // input data // 输入数据
[2]textbox= "data" ; [2] textbox =“数据”; // 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. 当我选择dropdwn-list值3时,行插入了2次。 when select 5, inserted 3 times. 选择5时,插入3次。

You are closing connection only in catch block. 您仅在catch块中关闭连接。 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();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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