Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Saving to a mysql db from c# causing an exception

0

45 views

I am using a WPF window and trying to save an item into the database. I am working with three mysql database tables: Orders, Items, and Meals.

Items are related to both Meals and Orders. Orders table has an OrderID field, and Meals table has MealID. Items table has OrderID and MealID. I am trying to add meals to an order by adding a MealID and an OrderID into the Items table.

So far I had no problem adding entries into the other tables, but when I started trying to add an entry into a table that requires references from two other tables I got stuck. I am reading the MealID from a selected row in the grid that displays the Meals table. The OrderID can also be read from the order datagrid. The database db is imported as an EDO model. This is my code :

Meal selectedMeal = mealsDataGrid.SelectedItem as Meal; // select the meal from the grid
Order order = ordersDataGrid.SelectedItem as Order; // select the order from the grid
Item item = new Item(); // create new item
item.MealReference.Value = selectedMeal; // set the reference to the Meals table
item.OrderReference.Value = order; //set the reference to the Order table
db.AddItems(item); // insert into the Items table
db.SaveChanges(); // save to the database

When I run this I get an exception indicating that I am using the wrong mysql code. I also tried to create the items object hardcoding the values as in Item item = Item.CreateItem(1, 2) using the existing values for OrderId and MealID. Nothing seemed to work. (Similar code works fine with other tables that don't have two dependencies). If anyone has an experience with a similar setup please tell me what I am missing.

asked March 15, 2011 1:55 am CDT
posted via StackOverflow

0 Answers

Be the first to answer this question

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Saving pdf in postgres
February 9, 2011
Saving the End date
January 29, 2011