Hello everyone,
I have a small issue today and I`m hoping if someone can help me about it.
I have a table which is consist of three foreign key table.
It look like this:
ORDERS:
TransactionNumber (string)
OrderDate (DateTime)
PaymentFK (Foreing key to table Payments)
UserFK (Foreign key to table Users)
ShoppingCartFK (Foreign key to table Shopping Cart)
What I want is to save ShoppingCart into orders, so this is the code:
public void SaveOrder()
{
using (DataContext dc = new DataContext())
{
var a = (from x in dc.ShoppingCarts
where x.cartID == Session["LogggedUser"].ToString()
select x).SingleOrDefault();
var b = (from y in dc.Users
where y.name + " " + y.lname == Session["LoggedUser"].ToString()
select y).SingleOrDefault();
Orders myOrder = new Orders();
myOrder.TransactionNumber = lbl_br.Text;
myOrder.OrderDate = DateTime.Parse(lbl_date.Text);
myOrder.UserFK = b.Id;
myOrder.ShoppingCartFK=a.id;
myOrder.PaymentFK = int.Parse(dp_py.SelectedItem.Value);
dc.Orders.InsertOnSubmit(myOrder);
dc.SubmitChanges();
}
}
So, this works if logged user added
only one product in Shopping cart, but If he adds more items, the program fall (of course). I`ve tryed to do foreach loop like this:
foreach (ShoppingCart item in a)
{
}
But, acctually I`m not sure how to implement it.
Anyone have any suggestions? Is it possible, or should I make different arrangement in my database.
Best regards,
A.