none
Insert datacontext not working but also no error message? RRS feed

  • Question

  • Hello,

    why is the below insert statement not working? I see no new datarows in my table but i also get no error message in my IDE ?

     [Table]
        public class Klasse
        {
            [Column(Name = "klasse_id", DbType = "int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
            public int Klasse_id { get; set; }

            [Column(Name = "name", DbType = "nvarchar NOT NULL")]
            public String Name { get; set; }
        }


    public partial class KlassenDataContext : DataContext
    {
    public Table<Klasse> Klassen;

    public KlassenDataContext(String connectionString) : base(connectionString)
    {

    }
    }

    KlassenDataContext dataContext = new KlassenDataContext("Data Source=D:\\Privat\\test.sdf; password = 'xxx'");
    Klasse klasse = new Klasse();

    klasse.Name = "12222233";


    dataContext.Klassen.InsertOnSubmit(klasse);
    dataContext.SubmitChanges();


    Tuesday, September 23, 2008 9:47 PM

Answers

  • The output written to the log shows that the insert is happening. Does the insert statement appear correct to you?  After you call SubmitChanges() the primary key field of the entity should now contain the server generated ID.

     

    The best way to verify that the insert correctly executed in the database is to create a new DataContext and read the entities out of that table. Is the new one included?

    Friday, September 26, 2008 2:01 AM
    Moderator

All replies

  • The common cause for this is that you sdf file is being copied to your run directory when the project is run in the IDE.  This copied file is the one where the inserts are occuring, yet you are viewing the original one seeing no inserts.  There is a project setting associated with the DBML file that controls this.

     

    Tuesday, September 23, 2008 11:39 PM
    Moderator
  •  Matt Warren - MSFT wrote:
    The common cause for this is that you sdf file is being copied to your run directory when the project is run in the IDE.  This copied file is the one where the inserts are occuring, yet you are viewing the original one seeing no inserts.  There is a project setting associated with the DBML file that controls this.

     



    1. if the sdf file is copied to the run directory when the project is running in the IDE how can I then access the temp sdf database? Doing a search for an .sdf file while running the application in the IDE , returns no results?

    2. I have no DBML file. I made my Entities manually. I did not use the linq to sql designer because I use Sql Compact edition.
    Wednesday, September 24, 2008 10:01 AM
  • Can you try a little logging to see if the insert is being fired? datacontext.Log will help you capture the queries to server

    e.g. context.Log = Console.Out;

     

    also log  the connectionstring details from the context

    e.g. db.Connection.DataSource etc to verify which .sdf file is it hitting.

     

    Also if you query back using the same datacontext after "submitchanges() " do you get the newly added entity back.

     

    ~Deepti

     

     

    Thursday, September 25, 2008 5:33 PM
  • thats the important data from the console.out i have cut you are probably interested in not?



    INSERT INTO [Klasse]([name])
    VALUES (@p0)
    -- @p0: Input String (Size = 0; Prec = 0; Scale = 0) [12222233]
    -- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 3.5.30729.1

    SELECT CONVERT(Int,@@IDENTITY) AS [value]
    -- @ROWCOUNT: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
    -- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 3.5.30729.1



    e.g. db.Connection.DataSource etc to verify which .sdf file is it hitting.**i dont use a datasource at the moment**

     

    Also if you query back using the same datacontext after "submitchanges() " do you get the newly added entity back.** How can i check wether i get the newly added entity back? must still learn about that... **


    With pure sql i could insert data in the .sdf file without any problem.

    With linq statements i can NOT insert data in the .sdf file... ??


    Thursday, September 25, 2008 9:53 PM
  • The output written to the log shows that the insert is happening. Does the insert statement appear correct to you?  After you call SubmitChanges() the primary key field of the entity should now contain the server generated ID.

     

    The best way to verify that the insert correctly executed in the database is to create a new DataContext and read the entities out of that table. Is the new one included?

    Friday, September 26, 2008 2:01 AM
    Moderator

  • yes the input and output of latest dataset is ot the problem hehe the problem WAS and i dont have any clue why it got solved... the problem was that i insert dataset into the database but when i checked the DB`s data in VS08 IDE there was no change when i hit the button "execute sql" so i thought my .sdf file is not really used as i couldnt see any change... but now it works its very odd it just works lol, thx though!

    Friday, September 26, 2008 9:05 PM
  •  Matt Warren - MSFT wrote:
    The common cause for this is that you sdf file is being copied to your run directory when the project is run in the IDE.  This copied file is the one where the inserts are occuring, yet you are viewing the original one seeing no inserts.  There is a project setting associated with the DBML file that controls this.

     



    Matt, that's exactly what was happening to me and what was driving me crazy Smile

    Do you know where that setting is? I cant find it anywhere

    Regards and thanks a lot

    Dote

    Monday, October 13, 2008 4:02 PM