1

I am facing issue in submit changes in table using linq code executed successfully by it is not saving changes in table.

Some one says your table must have primary key to submit changes but my table have primary key.

Some one says use integrated security = true to but it generate exception invalid keyword integrated security

Here is my connection string

<add name="POSConnectionString" connectionString="Persist Security Info=False; Data Source=|DataDirectory|\App_Data\POS.sdf; mode=Read Write;"
    providerName="Microsoft.SqlServerCe.Client.4.0" /> 

Code to insert data in table

public bool InsertItem(Item item)
{
    try
    {
        using (SqlCeConnection conn = new SqlCeConnection(connectionString))
        {
            using (POSContext db = new POSContext(conn))
            {                        
                db.Items.InsertOnSubmit(item);
                db.SubmitChanges();
                return true;
            }
        }
    }
    catch (Exception ex)
    {
        return false;
    }

}

Here is my table script

CREATE TABLE [Item] (
  [ItemID] int IDENTITY (1,1) NOT NULL
, [ItemName] nvarchar(100) NOT NULL
, [ItemCode] nvarchar(100) NOT NULL
, [Weight] int NOT NULL
, [IsActive] bit NOT NULL
, [Stock] int NULL
, [UnitID] int NULL
);
GO
ALTER TABLE [Item] ADD CONSTRAINT [PK_Item] PRIMARY KEY ([ItemID]);
GO

I am working in windows form application development Database Sql server compact database 4.0 Visual studio 2012

Muhammad zubair
  • 291
  • 2
  • 6
  • 20

3 Answers3

2

I got the solution by changing database properties to don't copy and it works as it making a copy of database in bin/debug and saving changes there

Muhammad zubair
  • 291
  • 2
  • 6
  • 20
0

Do you have an auto-increment (Identity) column in your table. In order to insert data using Linq you must have an Identity column as Primary Key on your table.

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
0

I had the same problem. So, when you build your project, Visual Studio overwrite your database and it has dafault values. Try to launch it from "bin/debug" in your project directory. Hope it will be helpful.