ASP.NET offers a variety of tools and mechanisms for working with database data, including a number of data source controls, such as the SqlDataSource,
ObjectDataSource, and LinqDataSource, among others. The SqlDataSource is one of the most basic data source controls as it operates directly against
a configured database. Using the SqlDataSource control, an ASP.NET developer can retrieve, insert, update, or delete data by simply setting a few
properties. Little to no code is needed.
While the SqlDataSource makes it a walk in the park to implement the most common data access scenarios, a little extra effort is needed for more intricate
scenarios. One such data access pattern is retrieving the value of the just-inserted record's ID field, where the ID field is an IDENTITY
column. (An IDENTITY column is a numeric column in a SQL Server database table that has its value automatically assigned when a new record
is added to the table. IDENTITY columns are sometimes referred to as auto-number columns, as well.) Being able to get the ID value of the
just-inserted record is helpful in cases where you need to insert a new record and then insert other records into related tables, or when you want to
let the user start working with the just-added record, which might entail taking them to a URL like EditRecord.aspx?ID=justInsertedRecordID.
This article shows how to use the SqlDataSource control to insert a new record and retrieve the value of its ID field. In particular, we will look at
two examples: one that uses a stored procedure to insert the new record and another that uses an ad-hoc INSERT statement. Read on to learn more!
Read More >