2

I am trying to add a product to the Cart, but it returns the following:

An error occurred while updating the entries. See the inner exception for details.

Inner exception: System.Data.SqlServerCe.SqlCeException (0x80004005): A duplicate value cannot be inserted into a unique index. [ Table name = Cart,Constraint name = PK_Cart_0000000000000997 ] at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

Stack trace at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Objects.ObjectContext.SaveChanges() at JTS.Security.Identity.Push.ToCart(Int32 userId, String partNumber, String productDescription, Int32 quantity, Decimal price, Decimal lineTotal, String orderId, DateTime dateTime, Boolean isBoxed) in c:\Users\Jase\Documents\Visual Studio 2012\WebSites\One\App_Code\JTS.cs:line 251

The thing is though, I don't have any Unique indexes. And I even tried removing all primary keys (just to see if that would change anything) - which it didn't.

The code:

public bool ToCart(int userId,
                    string partNumber,
                    string productDescription,
                    int quantity,
                    decimal price,
                    decimal lineTotal,
                    string orderId,
                    DateTime dateTime,
                    bool isBoxed)
                {
                    bool addedToCart = false;

                    try
                    {
                        Cart cart = new Cart()
                        {
                            UserId = userId,
                            PartNumber = partNumber,
                            Description = productDescription,
                            Quantity = quantity,
                            Price = price,
                            LineTotal = lineTotal,
                            OrderId = orderId,
                            OrderDate = dateTime,
                            IsBoxed = isBoxed
                        };

                        database.AddToCarts(cart);
                        database.SaveChanges();

                        addedToCart = true;
                    }
                    catch (Exception exception)
                    {
                        addedToCart = false;
                        //Response(exception.Message);
                        addToCartExceptionDetails = exception.Message +
                            Environment.NewLine + Environment.NewLine + "Inner exception" +
                            exception.InnerException +
                            Environment.NewLine + Environment.NewLine + "Stack trace" +
                            exception.StackTrace;
                    }

                    return addedToCart;
                }

Also, if I create a new Order (which creates a new OrderID), it WILL add one item to the cart. BUT, It will never add anymore than 1 item to the cart under an existing OrderId!

What am I doing wrong? Why isn't this working?

Table Definition
Column Name   Data Type   Length   Allow Nulls     Unique    Primary Key
UserId        int         4        Yes             No        No
OrderId       nvarchar    1000     No              No        Yes
jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • Please add your tables structure – Amiram Korach Aug 09 '12 at 15:02
  • I just did, though new users can't add images, so I'm typing it all in a separate tab. – jay_t55 Aug 09 '12 at 15:03
  • 1
    If OrderId is the PK, how do you plan to insert more than one product to the cart? – Amiram Korach Aug 09 '12 at 15:11
  • 2
    If you've got a problem with a question, the least you could do is explain why, otherwise you'll always have problems with questions that - in your opinion - have problems. – jay_t55 Aug 09 '12 at 15:12
  • I changed it so it wasn't primary key - but it still gives the same error. And I've since (like I said in my question) removed all primary keys - and I still receive the same error – jay_t55 Aug 09 '12 at 15:13
  • the error code tells you which unique contraint you are violating: `Table name = Cart Constraint name = PK__Cart__0000000000000997 ` – Ghost Aug 09 '12 at 15:14
  • I seriously doubt you sucessfully removed the PK and all unique constraints and still got that error. If the error references a SQL table constraint then that table has that constraint. – paparazzo Aug 09 '12 at 15:15
  • The table does not have the Constraint. I'm beginning to think that this is a VS or EF bug, where it does not update the underlying code; problem is - I can't remember where the underlying code is. – jay_t55 Aug 09 '12 at 15:17
  • If the error code says you're violating a unique constraint, and tells you what table and what constraint, your conclusion is that it's a VS or EF bug? – Ghost Aug 09 '12 at 15:19
  • Because I am using VS 2012 RC on Windows 8 release PREVIEW- it's _definitely going to have bugs_, and I've already run into 2 known bugs with EF and 4 with VS. – jay_t55 Aug 09 '12 at 15:23
  • Thanks for your help everyone. I've managed to solve/overcome this issue, by `deleting databasename.edmx from the App_Code folder, and removing relevant entries from Web.config file; then Adding Entity Model all over again.` I have no idea what caused the issue, but I have a feeling that when a column in the db was changed, it didn't update it somewhere else, and deleting/re-adding everything again forced the new changes. – jay_t55 Aug 09 '12 at 15:41

1 Answers1

0

If you have a table where you deleted and re-added content, you can get this error while inserting new rows with an ID equal to existing IDs why dont you check this Link form more info

Community
  • 1
  • 1
Hussein Khalil
  • 1,395
  • 11
  • 29