I am in the process of migrating my code from Visual Studio 2010 to 2012 and making sure all runs as expected. During this transition, my LINQ-to-SQL sample program fails when trying to Insert data to one of my tables. Below is the code when the user clicks the save button:
public class DebugTextWriter : TextWriter
{
public override void Write(char[] buffer, int index, int count)
{
Debug.Write(new String(buffer, index, count));
}
public override void Write(string value)
{
Debug.Write(value);
}
public override Encoding Encoding
{
get
{
return Encoding.Default;
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
//First we locate the 'orderViewSource' object that the user filled in
CollectionViewSource orderViewSource =
FindResource("orderViewSource") as CollectionViewSource;
//Next we save the values entered in into a generic list (only 1 entry
here)
List<Order> ordList = orderViewSource.Source as List<Order>;
//Again, since there is only one entry, we take it and fill in a new row in
//our 'Order' table
Order ord = ordList.FirstOrDefault();
//In order for the insert to work, you need to create an instance of the
//LINQ to SQL class for that table and then call the insert method on the
//'ord' container and then finally submit it.
var ctx = new MyShopDataContext();
ctx.Orders.InsertOnSubmit(ord);
ctx.SubmitChanges();
MessageBox.Show("Order Saved!");
}
catch (Exception)
{
}
}
My code build without errors and there isn't an exception thrown. Any thoughts to why this code will not work?