Wrox Programmer Forums
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2004, 09:17 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Idenity Field

I have a simple table called "Groups" which has only 2 fields called GroupID (idenity field...increments by 1) and groupname.

It's a simple operation to add a record to the table, but I need to retrieve the value given to the identity field after the insert so I can use it in another related table.

Is there any way to have the Insert statement return the value from the identity field of the added record?

I remember that this was possible in ADO....how about ADO.Net?

Thanks.
 
Old September 20th, 2004, 02:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This functionality is not a function of ADO/.NET, it's up to the database system. If you are using MSSQL server, you can execute your insert together with a select and get the result:

<insert statement>; SELECT @@IDENTITY;

This will return you the last identity inserted which will correspond with the insert statement at the beginning.
 
Old September 21st, 2004, 07:34 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. But I'm still a little confused about how exactly I would retrieve the Identity value after the insert.

I'll try and paste my insert code below:

'insert into Groups : UserID, GroupName
            Dim MySQL As String = "Insert into Groups(UserID,GroupName) " & _
            "Values(@userid,@groupname)"

            Cmd = New SqlCommand(MySQL, SqlConnection1)

            With Cmd.Parameters
                .Add(New SqlParameter("@userid", UserID))
                .Add(New SqlParameter("@groupname", TextBox1.Text))
            End With

            SqlConnection1.Open()

            Try
                Cmd.ExecuteNonQuery()
            Catch
            End Try

            SqlConnection1.Close()

 
Old September 23rd, 2004, 10:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Dim MySQL As String = "Insert into Groups(UserID,GroupName) " & _
"Values(@userid,@groupname); SELECT @@ IDENTITY;"

Cmd = New SqlCommand(MySQL, SqlConnection1)

With Cmd.Parameters
    .Add(New SqlParameter("@userid", UserID))
    .Add(New SqlParameter("@groupname", TextBox1.Text))
End With

SqlConnection1.Open()

Try
    newID = Cmd.ExecuteScalar()
Catch
End Try

SqlConnection1.Close()


This runs the insert AND the select to retrieve the last identity. None of this is a function of ADO.NET, but of SQL. As I stated earlier this will work in MS-SQL Server. You will need to use other syntax (if available) for other database engines. If you aren't using MSSQL, then I can't answer the question. This is how I always do this because my inserts are usually made with a straight SQL command execution. There will be other methods when you are using datasets and use the methods of the dataset class to insert data.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace Gridview field if null with new field Indo77 ASP.NET 2.0 Basics 1 June 18th, 2007 06:22 AM
Updating a Date field based on another field arholly Access VBA 6 November 22nd, 2006 11:19 AM
Copy previous field record if next field is null ecampos Access VBA 6 June 23rd, 2006 12:55 PM
Date Field from Parameter Field narooma.12 Crystal Reports 0 November 14th, 2004 08:51 PM
Update city field based on zip field nganb SQL Server ASP 0 April 22nd, 2004 10:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.