Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Get Current Identity value of the table using LINQ.

Get Current Identity value of the table using LINQ.

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelpquestion
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    madhubabu
    wrote on last edited by
    #1

    Hi, I am working on a C#/.NET application where I need to get Current Identity value of the table using LINQ. Or else I can use the sql query to get the task done by either using var num = DataContext.ExecuteCommand("Select IDENT_CURRENT ('table')");, by using this code, i am getting the value = -1. or var num = DataContext.ExecuteQuery(("Select IDENT_CURRENT ('table')");, by using this code, I am getting an exception "Specified cast is not valid." and the value remains NULL. Correct me if I am doing something wrong. let me know if there are any better ways to do this task. my scenario is different where Last few records in the table are deleted.............So in that scenario i will not be getting the right value needed. presently 30 records --> Max(id) gives me 30 4 records added to the table---> Max(id) gives me 34 Now i am deleting the last 4 records added this means that Current Identity value will be 34 but when i do Max(id) it gives me 30 but the correct value is 34.right? Thanks for ur help in advance.

    A 1 Reply Last reply
    0
    • M madhubabu

      Hi, I am working on a C#/.NET application where I need to get Current Identity value of the table using LINQ. Or else I can use the sql query to get the task done by either using var num = DataContext.ExecuteCommand("Select IDENT_CURRENT ('table')");, by using this code, i am getting the value = -1. or var num = DataContext.ExecuteQuery(("Select IDENT_CURRENT ('table')");, by using this code, I am getting an exception "Specified cast is not valid." and the value remains NULL. Correct me if I am doing something wrong. let me know if there are any better ways to do this task. my scenario is different where Last few records in the table are deleted.............So in that scenario i will not be getting the right value needed. presently 30 records --> Max(id) gives me 30 4 records added to the table---> Max(id) gives me 34 Now i am deleting the last 4 records added this means that Current Identity value will be 34 but when i do Max(id) it gives me 30 but the correct value is 34.right? Thanks for ur help in advance.

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2

      See if one of these work:

      int num = DataContext.ExecuteQuery<int>("SELECT IDENT_CURRENT('SomeTable')").First();
      Int16 num = DataContext.ExecuteQuery<Int16>("SELECT IDENT_CURRENT('SomeTable')").First();

      Though, like somebody else said in one of your other cross-posts, this may not be the best way to go about doing this. The ideal way depends on how you are inserting records.

      Driven to the ARMs by x86.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups