locked
Getting value -1 as last identity added to table using @@IDENTITY RRS feed

  • Question

  • hi Im using Visual Studio 2010 C# application.  I have created a TABLE Session, and have inserted the data using this code:

    DatabaseDataContext context = new DatabaseDataContext();
     context.Sessions.InsertOnSubmit(
              new Session
              {
               sessionName = name,
               sessType = type          });
              context.SubmitChanges();
                     

    int sessid = context.ExecuteCommand("Select @@IDENTITY;");  //Retrieves last ID entered..
    MessageBox.Show("New Session Id: " + sessid);

    The data gets updated OK and the ID field gets auto-incremented each time..    But when i try and read the session Id and show it in message the result is:

    "New Session Id: -1"

    In the database i created table:

    CREATE TABLE Session
    (
      sessionID INTEGER not null IDENTITY(1,1),
      sessionType NVARCHAR(20) not null,
      sessionName NVARCHAR(30) not null,
      PRIMARY KEY (sessionID),
    );

    Can anyone see what I have one wrong?  Thanks, Emack

    • Moved by Jackie-Sun Wednesday, March 30, 2011 3:33 AM (From:LINQ to SQL)
    Sunday, March 27, 2011 9:15 PM

Answers

  • Create the insert as an object;

    EquivalenceDBDataContext context = new EquivalenceDBDataContext();
                Session newSession = new Session  //Object. Table name: Session
                {
                    date = dt,
                    className = "Temp11"
                };

                context.Sessions.InsertOnSubmit(newSession);
                context.SubmitChanges(); ;
                sessID = newSession.sessionID;
                MessageBox.Show("Uploaded Successfully" + sessID);


    Emma Mc Macken
    • Marked as answer by Emack84 Tuesday, April 12, 2011 10:53 PM
    Tuesday, April 12, 2011 10:53 PM

All replies

  • Hello Emack,

     

    Thanks for posting in the LINQ to SQL Forum!

    According to your description, I think your issue has more relation with ASP.NET and LINQ to SQL. So I suggest you to post in the ADO.NET, Entity Framework, LINQ to SQL, NHibernate Forum for better support! And I will move it to the Off-topic forum.

    Thanks for your understanding and support!

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 30, 2011 3:32 AM
  • ok, thanks Jackie.

    Emack

    Wednesday, March 30, 2011 4:52 PM
  • Create the insert as an object;

    EquivalenceDBDataContext context = new EquivalenceDBDataContext();
                Session newSession = new Session  //Object. Table name: Session
                {
                    date = dt,
                    className = "Temp11"
                };

                context.Sessions.InsertOnSubmit(newSession);
                context.SubmitChanges(); ;
                sessID = newSession.sessionID;
                MessageBox.Show("Uploaded Successfully" + sessID);


    Emma Mc Macken
    • Marked as answer by Emack84 Tuesday, April 12, 2011 10:53 PM
    Tuesday, April 12, 2011 10:53 PM