Click here to Skip to main content
15,885,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:
C#
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.
Posted
Comments
Herman<T>.Instance 31-Jan-12 9:31am    
is your problem:
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();

foreach (ShoppingCart item in a)
{
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();

}
ellisbay 31-Jan-12 9:46am    
Not working.. in query "a" if I select SingleOrDefault() foreach loop doesn`t work.
And If I don`t select SingleOrDefault(), I have to write myOrder.ShoppingCartFK=item.id;
In second case it builds, but when I click on button show error on "dc.SubmitChanges()".

Another idea?
Amir Mahfoozi 8-Feb-12 0:18am    
What error it gives at "dc.SubmitChanges()" in second case ?
Dean Oliver 2-Feb-12 0:33am    
Foreach loop only works on collections that implement an enumerator SingleOrDefault() gets a specific instance in a collection. that's why your loop won't work.

Can you make these changes and try
C#
var a = from x in dc.ShoppingCarts
        where x.cartID == Session["LogggedUser"].ToString()
        select x;

then
C#
foreach (ShoppingCart item in a)
{
     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();
}
 
Share this answer
 
Hello, I`ve forgot to write that I`ve solved this problem..
What was actual mistake, I really forget now, but I`ll add solution when I finish my project and go through all code.
 
Share this answer
 

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