0

当相同的书名插入购物车时,我试图更新我的数据表。

public bool checkBook(DataTable dt, String title)
{
    bool returnval = false;
    try
    {

        foreach (DataRow dr in dt.Rows)
        {
            String checktitle = dr["Title"].ToString();
            if (title == checktitle)
            {
                int a = Convert.ToInt32(dr["quantity"].ToString());
                dr["quantity"] = a + 1;
                returnval = true;
            }

        }
    }
    catch (Exception ex)
    {
        //do something
    }
    return returnval;
}

数量的初始值为1,但是在提交按钮的时候,数量还是1,但是第三次​​进入的时候,数量才开始增加1。我不明白错误在哪里?

编辑::

protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
    String title = ((Label)(ListView1.Items[ListView1.SelectedIndex].FindControl("Title"))).Text; ;
    decimal price = decimal.Parse(((Label)(ListView1.Items[ListView1.SelectedIndex].FindControl("Price"))).Text);

    cart cart = new cart(title, price);

    if (HttpContext.Current.Session["Cart"] != null)
    {
        DataTable shoppingcart = (DataTable)HttpContext.Current.Session["Cart"];
        bool check = checkBook(shoppingcart, title);
        if (check != true)
        {
            ShoppingCart.DataSource = cart.cartrow(shoppingcart);
            ShoppingCart.DataBind();
        }
        else
        {
            // if is true, it suppose to increase the quantity here 

            }

        }
    }

    else
    {

        HttpContext.Current.Session["Cart"] = cart.shoppingCart();
        ShoppingCart.DataSource = cart.shoppingCart();
        ShoppingCart.DataBind();
    }

}

班级::

String title { get; set; }
decimal price { get; set; }
int quantity = 1;
DataTable CartTable;
DataRow tableRow;
public cart(String _title, decimal _price)
{
title = _title;
price = _price;
}
public DataTable shoppingCart()
{
CartTable = new DataTable("cart");

CartTable.Columns.Add("ID", typeof(Int32));
CartTable.Columns["ID"].AutoIncrement = true;
CartTable.Columns["ID"].AutoIncrementSeed = 1;

CartTable.Columns.Add("Title");
CartTable.Columns.Add("Price");
CartTable.Columns.Add("quantity");
CartTable.Columns["quantity"].DataType = typeof(Int32);

tableRow = CartTable.NewRow();
tableRow["Title"] = title;
tableRow["Price"] = price;
tableRow["quantity"] = quantity;
CartTable.Rows.Add(tableRow);
return CartTable;
}

public DataTable cartrow(DataTable _cart)
{

tableRow = _cart.NewRow();
tableRow["Title"] = title;
tableRow["Price"] = price;
tableRow["quantity"] = quantity;
_cart.Rows.Add(tableRow);
return _cart;

}
4

1 回答 1

2

您需要显示更多代码以确定问题所在。您是否绑定到数据表?单击按钮后是否要重新创建它?

无论如何,以下是一些代码,以显示更新按您的预期工作。请注意使用 System.Data.DataSetExtensions 中的通用字段扩展方法。

namespace StackOverflowTestCode
{
    using System.Data;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class RandomTests
    {
        [TestMethod]
        public void DataTableUpdate_Test()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add( "Title", typeof( string ) );
            dataTable.Columns.Add( "Quantity", typeof( int ) );

            dataTable.Rows.Add( "TitleOne", 0 );
            dataTable.Rows.Add( "TitleTwo", 0 );
            dataTable.Rows.Add( "TitleThree", 0 );

            DataRow[] rowsToUpdate = 
                dataTable.Select( "Title = 'TitleTwo'" );

            if( rowsToUpdate != null && rowsToUpdate.Length == 1 )
            {
                rowsToUpdate[ 0 ][ "Quantity" ] = 
                    rowsToUpdate[ 0 ].Field<int>( "Quantity" ) + 1;
            }

            // The right row was updated.
            Assert.AreEqual( 1, dataTable.Rows[ 1 ][ "Quantity" ] );

            // The other rows were not updated.
            Assert.AreEqual( 0, dataTable.Rows[ 0 ][ "Quantity" ] );
            Assert.AreEqual( 0, dataTable.Rows[ 2 ][ "Quantity" ] );
        }
    }
}

编辑

更新后,唯一立即让我感到奇怪且可能是错误来源的事情如下:

HttpContext.Current.Session["Cart"] = cart.shoppingCart(); 
ShoppingCart.DataSource = cart.shoppingCart();

你为什么要调用该方法两次?

于 2012-05-05T21:06:34.090 回答