Microsoft KB Archive/320141

= HOW TO: Retrieve an Identity Value from a Newly Inserted Record from SQL Server by Using Visual Basic .NET =

Article ID: 320141

Article Last Modified on 12/26/2003

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 2000 64-bit Edition
 * Microsoft Data Engine 1.0

-



This article was previously published under Q320141



For a Microsoft Visual C# .NET version of this article, see 320897.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Sample
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article describes how to retrieve the identity value when you add a record into a SQL Server table with an identity field.

back to the top

Requirements
This sample uses the Northwind database in SQL Server and retrieves the identity values only for DataTables with no child tables. It can also be used with MSDE; however, the Northwind database is not included with MSDE. For information about how to use this procedure with MSDE, see the Troubleshooting section of this article.

A SQL Server identity field is an auto number field that you can define an incremental value for. For this reason, you cannot insert or update a value in this field as long as Identity_ insert is off, which is the default for a SQL Server identity field.

back to the top

Sample
 Start Visual Studio .NET, and then create a new Visual Basic Windows Application project:  On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.  Drag a Button onto the form from the Windows Forms toolbox.  At the top of the code window, add the following line: Imports System.Data.SqlClient   Paste the following code onto the Button's click event: Dim ds As New DataSet

Dim cnNorthwind As New SqlConnection(&quot;server=(local);integrated security=sspi;database=Northwind&quot;) Dim cmSelect As New SqlCommand(&quot;select employeeid,firstname,lastname from employees&quot;, cnNorthwind)

Dim stInsert As String stInsert = &quot;insert into employees (firstname,lastname) values(@Firstname,@Lastname);select employeeid,firstname,lastname from employees where employeeID = @@identity&quot; Dim cmInsert As New SqlCommand

With cmInsert .CommandText = stInsert .CommandType = CommandType.Text .Connection = cnNorthwind .Parameters.Add(New SqlParameter(&quot;@firstname&quot;, Data.SqlDbType.VarChar, 25, &quot;firstname&quot;)) .Parameters.Add(New SqlParameter(&quot;@lastname&quot;, Data.SqlDbType.VarChar, 25, &quot;Lastname&quot;)) End With

Dim daNorthwind As New SqlDataAdapter With daNorthwind .SelectCommand = cmSelect .InsertCommand = cmInsert End With

daNorthwind.Fill(ds, &quot;employees&quot;) Dim dr As DataRow dr = ds.Tables(&quot;employees&quot;).NewRow dr(1) = &quot;John&quot; dr(2) = &quot;Doe&quot; ds.Tables(&quot;employees&quot;).Rows.Add(dr) daNorthwind.Update(ds, &quot;employees&quot;) ds.AcceptChanges Dim i As Int16 For i = 0 To ds.Tables(&quot;Employees&quot;).Rows.Count - 1 With ds.Tables(&quot;Employees&quot;) Debug.WriteLine(&quot;EmployeeID: &quot; & .Rows(i)(0).ToString) Debug.WriteLine(&quot;Employee Firstname: &quot; & .Rows(i)(1).ToString) Debug.WriteLine(&quot;Employee LastName: &quot; & .Rows(i)(2).ToString) End With Next i                   </li> Change the connection string to reflect your SQL Server or MSDE information.

</li> Run the application.

</li> Click the Button.

The information for the identity field of the newly inserted record is displayed.</li></ol>

back to the top

Troubleshooting
Before you use this procedure, with MSDE you must use Data Transformation Services (DTS) to import the Northwind database for SQL Server or Microsoft Access.

back to the top

<div class="references_section">