PlaceHolder复制按钮单击 - 插入错误太多参数

时间:2018-06-06 14:36:39

标签: c# asp.net

我让用户在需要时选择文本框的数量;但是如果用户选择3然后按钮,则点击计数显示6;因此将数据插入数据库表;它说它有太多的论点。如何阻止它复制并从populate函数传递数量。

using (SqlCommand comm = new SqlCommand("sp_CreateNumber", connection, transaction))
                    {
                        int cont = ContentPlaceHolder1.Controls.Count;

                        for (int i = 0; i < cont; i++)
                        {
                            TextBox tx = (TextBox)ContentPlaceHolder1.FindControl("tx" + i);
                            value = tx.Text;

                            int parsedValue;
                            if (!int.TryParse(value, out parsedValue))
                            {
                                lblError.Text = "Please enter only numeric values ";
                                return;
                            }
                            else
                            {
                                comm.CommandType = CommandType.StoredProcedure;

                                   comm.Parameters["@loadSheetNum"].Value = lblSheet.Text; 
                                comm.Parameters.Add("@Number", SqlDbType.NVarChar).Value = value;
                                comm.ExecuteNonQuery();
                            }


protected void Page_Load(object sender, EventArgs e)
{if(!IsPostBack){    
    populate();
    }
    }

 public void populate()
{

    int count = Convert.ToInt32(ddlCount.SelectedItem.Value);
    for (int i = 0; i < count; i++)
    {
        TextBox tb = new TextBox();
        tb.ID = "tx" + i;


        ContentPlaceHolder1.Controls.Add(tb);
        tb.MaxLength = 10;
        ContentPlaceHolder1.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
    }
}

1 个答案:

答案 0 :(得分:0)

通过在每次循环迭代后清除SqlCommand参数来解决问题。

comm.Parameters.Clear():