Solved

ASP.NET/ ADO.NET Access 97 Autonumber

Posted on 2004-04-21
5
351 Views
Last Modified: 2008-03-03
Hi,

I have an access 97 database that I am writing data to through .NET.  The table I'm inserting data into has an autonumber column for the primary key.  I need to have that primary key return to the .NET layer so I can do other things with it.  I have done my insert through a straight SQL call, and through a DataAdapter -- they both work fine.  However, I cannot get the Primary Key returned.  I found out that you cannot do a "Select @@IDENTITY" because it is Access 97.  How can I get the primary key for an newly inserted row effectively?

Thanks
0
Comment
Question by:srobia
5 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 43 total points
ID: 10882250
you can do "select YourAutonumber from Table"

where YourAutonumber is the name of the Autonumber field
0
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 41 total points
ID: 10882409
use the RowUpdated event from the OleDbDataAdapter:
(the code below is from MSDN)
...........................
<code to set up and adding data to dataset>
' Include an event to fill in the Autonumber value.

AddHandler catDA.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf OnRowUpdated)

catDA.Update(catDS, "Categories")
.........................

the event handler:
Private Shared Sub OnRowUpdated(sender As Object, args As OleDbRowUpdatedEventArgs)
    ' Include a variable and a command to retrieve the identity value from the Access database.
    Dim newID As Integer = 0
    Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY", nwindConn)

    If args.StatementType = StatementType.Insert
      ' Retrieve the identity value and store it in the CategoryID column.
      newID = CInt(idCMD.ExecuteScalar())
      args.Row("CategoryID") = newID
    End If
End Sub

hope this help
0
 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 41 total points
ID: 10883251
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

13 Experts available now in Live!

Get 1:1 Help Now