Solved

Handling SQL Server Identity columns within a Datatable

Posted on 2004-09-15
3
423 Views
Last Modified: 2012-05-05
Hello all,

I have a tabe in a SQL Server database with an identity column set as the primary key.  I'm loading this table into a datatable through the use of a sqldataadaptor.  The problem is this, when I add a row to the datatable the column is not incremented.  That column is just set to a null value.  I tried setting that column's autoincrement (along with the autoincrementseed) property but still no go.  Any ideas?

Thanks,

JD
0
Comment
Question by:mrbass78
  • 2
3 Comments
 
LVL 4

Expert Comment

by:gdexter
ID: 12067062
Use this method of the DataAdapter to retrieve the new value after an update:

Private Shared Sub DaAdapter_RowUpdated(ByVal sender As Object, ByVal e As   System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles DaAdapter.RowUpdated

        If (e.StatementType = StatementType.Insert) Then
            Dim cmd As New OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
           
           'Inser the name of your ID column
            e.Row("ID") = cmd.ExecuteScalar()
            e.Row.AcceptChanges()
        End If

    End Sub

'Now after the call to update you can get the value:
'sample code

dataTbl.Rows.Add(newRow)
DaAdapter.Update(ds, "YourTable")

Dim id as Integer = newRow.Item("ID")

0
 

Author Comment

by:mrbass78
ID: 12067093
Thanks for the help but is there a way you could do it without updating?  I want the user to be able to work using the table but if they exit without saving I don't want the changes made to the actual table in the database.  So is there a way to do it just in the datatable itself?
0
 
LVL 4

Accepted Solution

by:
gdexter earned 500 total points
ID: 12067380
Is the Identity column relevent to the user?
You should be able to add as many new rows as you would like and during the update allow SQL Server to take care of the ID column.

Remeber the Datatable is disconnected from the Database and has know way of knowing what the next ID is going to be because other users could be making inserts while you are disconnected, that is why the row updated event is their.

In addtion I gave you the OleDb version before: sorry,
Private Shared Sub DaAdapter_RowUpdated(ByVal sender As Object, ByVal e As   System.Data.SqlClient.SqlRowUpdatedEventArgs) Handles DaAdapter.RowUpdated

        If (e.StatementType = StatementType.Insert) Then
            Dim cmd As New SqlCommand("SELECT @@IDENTITY", e.Command.Connection)
           
           'Inser the name of your ID column
            e.Row("ID") = cmd.ExecuteScalar()
            e.Row.AcceptChanges()
        End If

    End Sub
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now