Mise à jour la valeur dans la datatable

J'ai essayé de mettre à jour ma datatable quand jamais le même titre du livre est inséré dans le panier.

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;
}

Valeur initiale dans la quantité est de 1, mais lorsque le bouton a été soumis, la quantité est toujours 1, mais quand il entre dans la troisième période, le nombre n'a commencé à augmenter par un. Je ne comprends pas où est l'erreur?

Édité ::

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

}

classe::

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;

}
  • Votre question n'est pas claire. Veuillez décrire plus. où est button, qu'entendez-vous par bouton soumis?
  • Votre question n'est pas claire. Fournir schema de votre datatable. Fournir le code complet avec votre balisage html.
  • J'ai trouvé un problème lorsque j'appuie sur le même livre d'ajout au panier, il n'ajoutez pas la première fois, mais quand j'ai pris la même url, et ouvert dans autre onglet, ajouter de un à deux? est-il un problème avec mon codage?
InformationsquelleAutor eugene | 2012-05-05