繁体   English   中英

我的数据集无法正常工作

[英]My dataset doesn't work properly

我的问题是,当它更新时,它一次又一次地添加其中的先前数据。

我使用Telerik网格视图

这是我的代码三层

第一

    private void btnSbmt_Click(object sender, EventArgs e)
    {
        foreach (var row in radGridView1.Rows)
        {
            _MyName.Add((string)row.Cells[1].Value);
        }

        foreach (var row in radGridView1.Rows)
        {   // 0 - first column
            _MyAmount.Add((int)row.Cells[2].Value);
        }

        foreach (var row in radGridView1.Rows)
        {
            _MyPrice.Add((decimal)row.Cells[3].Value);
        }

        Ref_View_Model = new View_model._View_Model();
        Ref_View_Model.GetInsertProduct(_myName, _myAmount, _myPrice, txtDt.Text);
        radGridView1.CurrentRow.Delete();
        productTableAdapter.Update(sales_and_Inventory_SystemDataSet);
        productTableAdapter.Fill(sales_and_Inventory_SystemDataSet.Product);
        MessageBox.Show("Product(s) were added", "Done", MessageBoxButtons.OK);}

第二个

        public void GetInsertProduct( List<string> _name, List<int> _amount, List<decimal> _price, string _date)
    {
        Ref_Model = new Model._Model();
        Ref_Model.InsertProduct( _name, _amount, _price, _date);
    } 

和第三个

     public void InsertProduct(List<string> _myName, 
                      List<int> _myAmount, 
                      List<decimal> _myPrice, string _date)

{Connection_String =我的连接字符串

Query = @"INSERT INTO dbo.product(Name, Amount, Price, [date]) 
                             VALUES(@Name, @Amount, @Price, @Date);";

using ( Con = new SqlConnection(Connection_String))
using ( Cmd = new SqlCommand(Query, Con))
{

    Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
    Cmd.Parameters.Add("@Amount", SqlDbType.Int);
    Cmd.Parameters.Add("@Price", SqlDbType.Decimal);
   // Cmd.Parameters.Add("@Date", SqlDbType.NVarChar);
    Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);

    Cmd.Connection = Con;
    Con.Open();

    int recordsToAdd = _myName.Count();
    for(int x = 0; x < recordsToAdd; x++)
    {
        Cmd.Parameters["@Name"].Value = _myName[x];
        Cmd.Parameters["@Amount"].Value = _myAmount[x];
        Cmd.Parameters["@Price"].Value = _myPrice[x];
        Cmd.Parameters["@Date"].Value = _date;
        Cmd.ExecuteNonQuery();
    }
}

}

似乎您正在使用全局变量来保留从网格读取的值。 如果您在第一次插入后仍未清除它们,那么这些值仍在全局列表中,然后将它们再次添加到数据表中

当然,您可以只使用一个循环就可以使用网格中存在的实际值重新加载全局变量。

private void btnSbmt_Click(object sender, EventArgs e)
{
    // This removes whatever is in the lists 
    _MyName.Clear();
    _MyAmount.Clear();
    _MyPrice.Clear();       

    // and now start adding items from scratch
    foreach (var row in radGridView1.Rows)
    {
        _MyName.Add((string)row.Cells[1].Value);
        _MyAmount.Add((int)row.Cells[2].Value);
        _MyPrice.Add((decimal)row.Cells[3].Value);
    }
    ....

暂无
暂无

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

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