Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Handling SQL Server Identity columns within a Datatable

Posted on 2004-09-15
3
451 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 29
Convert datetime to time string 10 28
Visual Studios 1 76
Name Space error VS2015 1 22
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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