Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
The two table i am talking abour linked by there ID which is Order.Order_ID and Order_Item.Order_ID

i try to do many solution in the internet and it not seem to work. my code just insert all the data i

need into that two table but ! Order_Item.Order_ID is still null. can anyone help please.

PS. the Order.Order_ID is identity specification so it will get the id only when the data is insert.

Here is MY Code

C#
private void addReceiptCommandClick(object obj)
        {

            Order neworder = new Order();

            neworder.A_ID = admin_Name.A_ID;
            neworder.O_Date = o_Date;
            neworder.Order_ShowName = order_ShowID;
            neworder.Order_Price = order_NetPrice;
            neworder.O_Status = Status.StatusX;
            Context.Add(neworder);
            //selectedsupplier = Context.Suppliers.Where(S => S.Sup_Name == sup_Name).FirstOrDefault();
            neworder.Sup_ID = s_Name.Sup_ID;
            Context.Add(neworder);
            //Context.SaveChanges();
            foreach (Order_Item item in Order_Item)
            {
                
                item.O_ID = neworder.Order_ID;
                selectedproduct = Context.Products.Where(Pro => Pro.P_ShowID == item.P_Code).FirstOrDefault();
                Lot_Stock newlot = new Lot_Stock();
                newlot.Lot_Name = item.Lot_Code;
                newlot.Price = item.Price;
                newlot.Date = o_Date;
                newlot.Quantity = item.Quantity;
                neworder.Order_Items.Add(item);
                
               //newdetail.Order_ID = neworder.Order_ID;
            }
            Context.SaveChanges();
        }
Posted
Updated 13-Sep-14 9:09am

1 solution

Consider that you make item.O_ID = neworder.Order_ID which has no value assinged ,
if neworder.Order_ID is Identity you have to get it before assigning it's value to OrderItem

private void addReceiptCommandClick(object obj)
{

Order neworder = new Order();
neworder.Order_ID = Context.Order.Max().ID+1; // will get the value if column is not identity
neworder.A_ID = admin_Name.A_ID;
neworder.O_Date = o_Date;
neworder.Order_ShowName = order_ShowID;
neworder.Order_Price = order_NetPrice;
neworder.O_Status = Status.StatusX;
Context.Add(neworder);
//selectedsupplier = Context.Suppliers.Where(S => S.Sup_Name == sup_Name).FirstOrDefault();
neworder.Sup_ID = s_Name.Sup_ID;
Context.Add(neworder);
//Context.SaveChanges();
foreach (Order_Item item in Order_Item)
{

item.O_ID = neworder.Order_ID;
selectedproduct = Context.Products.Where(Pro => Pro.P_ShowID == item.P_Code).FirstOrDefault();
Lot_Stock newlot = new Lot_Stock();
newlot.Lot_Name = item.Lot_Code;
newlot.Price = item.Price;
newlot.Date = o_Date;
newlot.Quantity = item.Quantity;
neworder.Order_Items.Add(item);

//newdetail.Order_ID = neworder.Order_ID;
}
Context.SaveChanges();
}
 
Share this answer
 
v2
Comments
Real_Criffer 14-Sep-14 9:46am    
well will the database allow us to insert the ID if it is identity
[no name] 15-Sep-14 4:26am    
note that if the column is identity you just need to get its values not to set it
if not identity u need to set it's value by Context.Order.Max().ID+1
[no name] 15-Sep-14 4:29am    
Check my answer above as it's modified

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