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. C#
  4. Updating Child Table's Key (almost have it)

Updating Child Table's Key (almost have it)

Scheduled Pinned Locked Moved C#
databasequestionannouncement
1 Posts 1 Posters 0 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
    MaxRelaxman
    wrote on last edited by
    #1

    I'm trying to use a SqlDataAdapter to update a dataset containing multiple tables linked by a common parent table. I've set up my relations and ALMOST everything is working except for when my RowUpdated event handler tries to retrieve the identity field. Here's my code (without the try/catch/finally stuff and sanitized field names) private void ParentRowUpdated(object sender, SqlRowUpdatedEventArgs e) { SqlCommand dbCommand = new SqlCommand("SELECT @@Identity"); SqlConnection cn = new SqlConnection(GetConnString(m_strConnStringName)); using (SqlConnection dbConn = new SqlConnection(GetConnString(m_strConnStringName))) { using (SqlDataAdapter daAdapter = new SqlDataAdapter()) { dbCommand.Connection = e.Command.Connection; daAdapter.SelectCommand = dbCommand; e.Row["p_key"] = Int32.Parse(daAdapter.SelectCommand.ExecuteScalar().ToString()); e.Row.AcceptChanges(); } } } Using @@Identity in my select query does retrieve the correct Identity value. However, when I try using SCOPE_IDENTITY() I get a blank value returned. I don't like using @@Identity because this will be a multi user system so I'm worried about getting the wrong value. Does anyone have any idea of a better way to do this? Thanks!

    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