Solved

Handling SQL Server Identity columns within a Datatable

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 52
VS.net 2010 11 34
.NET tools for adding thread safety to a web app? 3 33
Winform Module - What is the ASP.Net equiv 2 20
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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