Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables:
Order(id,date,idWordker,idCustomer) Primakey key:id
OrderDetail(id,idProduct,quantity,price) Primakey key:id
You can see 2 keys in OrderDetail.I need to update the quantity
I use:
C#
public DataTable AddCart(string id, string idProduct,int quantity,int price)
{
        DataTable tbCart = new DataTable();
        tbCart = (DataTable)HttpContext.Current.Session["Cart"];
        if (tbCart == null)
            tbCart = MakeCart();
        DataRow dr = tbCart.NewRow();
        dv = tbCart.DefaultView;
        dv.Sort = "idProduct";
        rIndex = dv.Find(idProduct);
        if (rIndex != -1)
        {
            //update quantity
        }
        else
        {
                dr[0] = id;
                dr[1] = idProduct;
                dr[2] = 1;//quantity
                dr[3] = price;
        }
}

If OrderDetail exist idProduct,the quantity of product will be updated.Session["Cart"] will be insert into OrderDetail
How to find with 2 keys?
Posted

2 keys?

Which one of them is a unique reference point for a product? Pick and use the one that is unique based on your DB design and use. If both are unique, remove one key as it's not needed.
 
Share this answer
 
Comments
giatuan2011 5-Jun-12 5:17am    
They are primakey key.
I think the OrderDetail has more products
Sandeep Mewara 5-Jun-12 5:44am    
2 primary key?
giatuan2011 5-Jun-12 5:36am    
I just added:
tbCart.Rows[rIndex][2]=int.Parse(tbCart.Rows[rIndex][2].ToString())+slxuat;
But I have new problem.I need to create the "id" after assigning tbCart to Session ["Cart"].This only happens 1 time.I tried Random.It give me more id.How to make auto id?
Sandeep Mewara 5-Jun-12 5:45am    
Set the column in DB as IDENTITY column and set auto increment to 1. This will automatically create ID in incremental manner whenever you insert a record in your database.
Espen Harlinn 5-Jun-12 9:23am    
Good points :-D
As stated in the question only Id is the primary key in OrderDetail and idProduct is another id.
Hence, I think the Select method of DataTable can be used to find the required row as follows:
C#
DataRow[] rows = tbCart.Select(
        string.Format("id='{0}' and idProduct='{1}'",id,idProduct),
        idProduct, DataViewRowState.CurrentRows);
if (rows.Length > 0){
   rows[0]["quantity"]= 
   rows[0]["price"]=
}
 
Share this answer
 
Comments
giatuan2011 5-Jun-12 5:38am    
Your solution is work.
But I have new problem.I need to create the id after assigning tbCart to Session ["Cart"].This only happens 1 time.I tried Random.It give me more id.How to make auto id?
VJ Reddy 5-Jun-12 5:42am    
The Guid.NewGuid().ToString() can be used to generate a unique ID.
Espen Harlinn 5-Jun-12 9:23am    
Neat :-D
VJ Reddy 5-Jun-12 9:40am    
Thank you, Espen :)
Maciej Los 5-Jun-12 14:50pm    
Good answer, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900