Results 1 to 4 of 4

Thread: Retrieving Primary Key

  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2002
    Location
    Nashville, TN
    Posts
    103

    Question Retrieving Primary Key

    I have a form that is managing and updating data in the dataset. When I write the data back to my table using the update method, I need to refresh the dataset so that I can get the identity value that was inserted into the table (if it was a new record). I tried using the AcceptChanges method but that didn't seem to work. Any other ideas?

    Thanks

  2. #2
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    Shane,

    Add a handler for the data adapters row updated event. Point it too a sub that uses SQL and Accesses @@Select statement. (Select statement gets the ID of the last record to be inserted or modified)

    ie.

    VB Code:
    1. AddHandler myAdapter.RowUpdated, AddressOf mySub
    2.  
    3. ....
    4.  
    5. Private Sub mySub(ByVal sender As Object, ByVal e As SqlClient.SqlRowUpdatedEventArgs)
    6.  
    7. Dim cmdIdentity As New SqlClient.SqlCommand("Select @@Identity", sqlConnection)
    8.             Dim Row As DataRow
    9.             Dim RowID As Integer
    10.  
    11.             'Get the row that is being updated in the DataSet
    12.             Row = e.Row
    13.  
    14.             Try
    15.                 'Get the updated row ID from the database
    16.                 RowID = cmdIdentity.ExecuteScalar
    17.  
    18.                 'Update the datatable row
    19.                 Row.Item("myColumn") = RowID
    20.             Catch ex As Exception
    21.                 Throw New Exception("Unable to retrieve ID")
    22.             End Try
    23.  
    24. End Sub

    This code will fire whenever a row is being updated by the data adapter, and change the ID of the row right after the update in the dataset.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Mar 2002
    Location
    Nashville, TN
    Posts
    103
    That didn't work. It throw an exception stating the it could not convert type DBNull to Long which means the "Select @@Identity" is returning null. Why doesn't the acceptchanges work? I thought the dataset would refresh itself from the dataprovider when you called the acceptchanges method.

  4. #4
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    Accept changes won't work because a datatable is not connected to the database in any form. Hence the connectionless nature of Datasets/Tables. The only way the datatable knows it's being updated is thru the dataadapter. (ie when you use the data adapters update command via the data table)

    @@Identity has always worked fine for me in Access and SQL. I suggest you preform an update or insert on the database, and then if using SQL use Query Analyzer and the Identity command in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width