Hey everyone, I know there are literally thousands of questions and answers on the internet for getting the SCOPE_IDENTITY(), but I haven't found a single one that relates to my particular situation... So here we go...

I've decided to use VS .NET's built in Data Components, which in my case doing things the mainstream easy way always turns out to be the hard way for me... So anyway, here's my VB .NET code... Notice the line that is commented, that's where I need to get the SCOPE_IDENTITY().

Private Sub SaveChangesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesToolStripMenuItem.Click

        If Integer.Parse(contactId) = 0 Then
                ' contactID = ''' Some how need to get the Scope_Identity '''
            Catch ex As System.Exception
            End Try
            Catch ex As System.Exception
            End Try
        End If

        CRM_ContactsDataGridView.Visible = True
        editDetailsViewPanel.Visible = False
        detailsViewPanel.Visible = True
        SaveChangesToolStripMenuItem.Visible = False
        CancelChangesToolStripMenuItem.Visible = False
        AddContactToolStripMenuItem.Visible = True

    End Sub

Recommended Answers

Retrieving Identity or Autonumber Values

A bit of a pain to do with using a dataadapter to execute the queries but the above link will show you an example of doing so.

Jump to Post

All 2 Replies

You don't get it with a table adapter :) If you use a typed dataset it will:
Insert the record (using your insert query)
Get the scope identity
Select your record where id = scope_id() (using your select query)

I find it rather harder to use the built in datasets for the most part. There are a few exceptions like when I need to see the data at design time, but other than that I don't use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.