I'm in the middle of creating final project "Restaurant Information System" using C# Windows Application. I've a problem when I would like to save order transaction to database using Typed DataSet
There are 2 datagridview, left one to display all menu and right one to store the order details. I've displayed all menu in the left one and when I try to add menu to right one, it's failed. It is failed because I try to add menu to order detail but the order id not set.
The Database structure:
- Table Menu : ID (PK), Code, Name, Price
- Table Order : ID (PK), Date, Total
- Table Order_Detail : ID (PK), Order(FK), Menu(FK), Quantity, Price
and the Typed DataSet has same structure.
I create the project in 3 Tier Architecture using this Walkthrough: Creating N-Tier Data Application (
http://msdn.microsoft.com/en-us/library/bb384570.aspx)
updated: I have succeed to add order detail(menu) to right one, but with a little tricky, I create the OrderRow, set its attribute manually, and add to OrderDataTable I create the OrderDetailRow, and set the OrderDetailRow.Order = OrderRow and rest manually but, menu code and menu name do not show in OrderDetail DataGridView
The code:
RestaurantDataSet.OrderRow orderRow = restaurantDataSet.Order.NewOrderRow();
RestaurantDataSet.OrderDetailRow newOrderDetail = restaurantDataSet.OrderDetail.NewOrderDetailRow();
DataRowView dataRowView = (DataRowView)menuBindingSource.Current;
RestaurantDataSet.MenuRow menuRow = (RestaurantDataSet.MenuRow)dataRowView.Row;
newOrderDetail.OrderRow = orderRow;
newOrderDetail.Order = orderRow.ID;
newOrderDetail.Menu = menuRow.ID;
newOrderDetail.Price = menuRow.Price;
newOrderDetail.Quantity = (int)nudQuantity.Value;
orderRow.Total = newOrderDetail.Quantity * newOrderDetail.Price;
restaurantDataSet.OrderDetail.Rows.Add(newOrderDetail);
Furthermore, I try to save the transaction based on article Walkthrough: Saving Data from Related Data Tables (Hierarchical Update) (
http://msdn.microsoft.com/en-us/library/bb384432) and Walkthrough: Saving Data to a Database (Multiple Tables) (
http://msdn.microsoft.com/en-us/library/4esb49b4) but still failed, I've got error "The INSERT Statement conflicted with the FOREIGN KEY constraint "FK_OrderDetail_Order". The conflict occurred in database "RestaurantDB", table "dbo.Order", column 'ID'. The statement has been terminated."
The code:
RestaurantDataSet.OrderDetailDataTable newOrderDetails = (RestaurantDataSet.OrderDetailDataTable)
restaurantDataSet.OrderDetail.GetChanges(DataRowState.Added);
try
{
// Update the Order table.
OrderTableAdapter orderAdapter = new OrderTableAdapter();
orderAdapter.Update(restaurantDataSet.Order);
if (newOrderDetails != null)
{
OrderDetailTableAdapter orderDetailAdapter = new OrderDetailTableAdapter();
orderDetailAdapter.Update(newOrderDetails);
}
restaurantDataSet.AcceptChanges();
}
catch (System.Exception ex)
{
throw ex;
}
If you have any sample or tutorial, please let me know.
Please don't down vote my question, I really need help. I've googling many times, and not found many tutorial explain N-Tier with Typed Dataset.
Thanks,
Willy