none
insert does not work RRS feed

  • Question

  • I am playing with LINQtoSQL and having problems with insert. It does not seem to work. Select queries work just fine.
    here is the code I am testing. No exception is being thrown, but nothing gets inserted either. Can somebody help:

    try

    {

     

    Customer cust = new Customer();

    cust.CompanyName =

    "company";

    cust.ContactName =

    "contact";

    cust.ContactTitle =

    "title";

    cust.City =

    "city";

    cust.Address =

    "address";

    cust.Region =

    "region";

    cust.PostalCode =

    "Post code";

    cust.Country =

    "country";

    cust.Phone =

    "phone";

    cust.Fax =

    "fax";

    cust.CustomerID =

    "myID";

     

    DataClasses1DataContext dc = new DataClasses1DataContext();

    dc.Customers.InsertOnSubmit(cust);

    dc.SubmitChanges();

    }

     

    catch (Exception ex)

    {

     

    this.txtMessage.Text = ex.Message;

    }

     
    Thursday, August 27, 2009 7:43 PM

Answers

  • I figured out what was causing the problem.

    In my posting I forgot to mention that I also had the following validation code:  

     

    partial class DataClasses1DataContext

       {

            partial void InsertCustomer(Customer instance)

            {

                if (instance.CompanyName == "y" && instance.ContactTitle == "y")

                    throw new System.InvalidOperationException();

     

                //base.ExecuteDynamicInsert(instance);

            }

        }

     

    It was missing the line that is shown as commented out:

     

    Obviously nothing was working. This is what happens when you blindly copy the sample code from documentation. The topic "How to: Add Validation to Entity Classes" has the following sample code, which is missing the base.ExecuteDynamicUpdate(instance) line:

     

    partial void UpdateCLASSNAME(CLASSNAME instance)

    {

        if ((instance.COLUMNNAME == x) && (instance.COLUMNNAME = y))

        {

            string ErrorMessage = "Invalid data!";

            throw new System.Exception(ErrorMessage);

        }

    }
    • Marked as answer by sapt Friday, August 28, 2009 3:08 AM
    Friday, August 28, 2009 3:08 AM

All replies

  • a little more would be helpful.  what type of error are you getting?  can you provide the stack?

    Happy Coding!
    kashif
    Thursday, August 27, 2009 8:59 PM
  • That's the problem- I get no errors but record does not get inserted.
    Thanks
    Thursday, August 27, 2009 9:08 PM
  • I figured out what was causing the problem.

    In my posting I forgot to mention that I also had the following validation code:  

     

    partial class DataClasses1DataContext

       {

            partial void InsertCustomer(Customer instance)

            {

                if (instance.CompanyName == "y" && instance.ContactTitle == "y")

                    throw new System.InvalidOperationException();

     

                //base.ExecuteDynamicInsert(instance);

            }

        }

     

    It was missing the line that is shown as commented out:

     

    Obviously nothing was working. This is what happens when you blindly copy the sample code from documentation. The topic "How to: Add Validation to Entity Classes" has the following sample code, which is missing the base.ExecuteDynamicUpdate(instance) line:

     

    partial void UpdateCLASSNAME(CLASSNAME instance)

    {

        if ((instance.COLUMNNAME == x) && (instance.COLUMNNAME = y))

        {

            string ErrorMessage = "Invalid data!";

            throw new System.Exception(ErrorMessage);

        }

    }
    • Marked as answer by sapt Friday, August 28, 2009 3:08 AM
    Friday, August 28, 2009 3:08 AM