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

5
  • Are you sure you're checking the right database? Jan 29, 2014 at 8:09
  • I am damn sure. I am getting the values from table perfectly only insert cause the issue and there is only one data base in my project as i am using sql compact database Jan 29, 2014 at 8:58
  • 2
    Look in your bin/debug folder for a copy of the database file
    – ErikEJ
    Jan 29, 2014 at 9:24
  • yes i got that it making a copy of database in bin/debug and saving changes there can you tell me how to solve this means it should save this in actual database Jan 29, 2014 at 13:38
  • i got the solution by changing database properties to don't copy and it works Jan 30, 2014 at 18:11

3 Answers 3

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

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.

3
  • yes i have primary key and auto increment and primary key column Jan 29, 2014 at 6:26
  • Try to remove your integrated security = true from connection string. Or create your connection again by removing current connection and replace your connection string with the new one.. Jan 29, 2014 at 6:30
  • SqlCeConnection is not taking integrated security = true as a keyword Jan 29, 2014 at 6:36
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.