Retrieving the Just-Inserted ID of an IDENTITY Column Using a SqlDataSource Control

Posted by: 4GuysFromRolla.com Headlines, on 01 Oct 2008 | View original | Bookmarked: 0 time(s)

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 >

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: SQL | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1536 | Hits: 11

Similar Posts

  • ClientIDMode in ASP.NET 4.0 more
  • A generic way to find ASP.NET ClientIDs with jQuery more
  • CodeDigest.Com Article,Codes,FAQs - April,2009 more
  • Easy way to create a web-based AJAX SFTP Client application more
  • Prototyping with SketchFlow more
  • Microsoft Ajax 4.0 Preview 4 now available more
  • Take Charge of Your Security more
  • Bulk Importing Content into a SharePoint Publishing Site more
  • Handling Formats Based On Url Extension more
  • SESSION NOTES: ASP.NET 4.0 Roadmap more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD