0

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?

Anthony Darden
  • 137
  • 1
  • 5
  • 13
  • 4
    how do you know there is no exception thrown? your swallowing any that might come out of that block. – Jay Jun 20 '13 at 15:19
  • Isn't the order in orderViewSource already an existing entity ? – Ondrej Svejdar Jun 20 '13 at 15:20
  • @Jay - good question, and my rookie status on matter of data handling is showing here, but when I run in debug it flows right through to "Order Saved!" and never goes to the catch line. – Anthony Darden Jun 20 '13 at 15:32
  • @OndrejSvejdar - The order I am trying to add with this code is unique and not a duplicate – Anthony Darden Jun 20 '13 at 15:35
  • Its hard to say without seeing where your data is coming from. You may want to pop up SQL Profiler and see if any SQL is getting executed. Also remove the `try...catch` completely. – Jay Jun 20 '13 at 15:39
  • 1
    If you don't don't know how to use profiler, you can log linq-to-sql query like this - create class 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; } } } and set ctx.Log to instance of this class. In debug, it will show the sql commands being executed. – Ondrej Svejdar Jun 20 '13 at 15:51
  • catch (Exception ex) { } - check what is 'ex' – cinek Jun 20 '13 at 15:51
  • @Jay - I removed the try...catch, but I do not have SQL Profiler since I am using the Express 2012 version that came with VS2012 Professional. I am trying the linq-to-sql logging that Ondrej Svejdar suggested and see what happens...although to where I am getting my data - from Server Explorer, Data Connections, I created a new connection and the name I gave it is 'MyShop.mdf' and added two table to it. – Anthony Darden Jun 20 '13 at 16:24
  • @OndrejSvejdar - I added the code you suggested, but it doesn't like the Debug.Write statements? Any suggestions to that? Thanks for the help, too! – Anthony Darden Jun 20 '13 at 16:25
  • Debug is defined in System.Diagnostics - add using System.Diagnostics; also you have to set the debug logger to your MyShopDataContext - ie. ctx.Log = new DebugTextWriter(); – Ondrej Svejdar Jun 20 '13 at 17:01
  • can you see the item in `cxt.GetChangeset()` after the insert but before the `.SubmitChanges()`? – Jay Jun 20 '13 at 20:05
  • @OndrejSvejdar - thanks again for your help, this helped identify that nothing is being based to the table...still not sure why but this is progress for me and I have more to dig into and keep learning :) – Anthony Darden Jun 20 '13 at 20:06
  • @Jay - I added that line, and when I view the contents of the Order table in this manner, I only see the existing two entries which I manually added earlier... – Anthony Darden Jun 20 '13 at 20:15

1 Answers1

0

My error on this question...I should have known better: after properly researching this website, I found the error resolution with the following:

"An attempt to attach an auto-named database" error

Thanks to all who posted

Community
  • 1
  • 1
Anthony Darden
  • 137
  • 1
  • 5
  • 13