spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
Related Items
 
Home arrow Articles arrow Visual Basic / .NET Articles arrow How to Get Identity Field Value without Using Stored Procedures

How to Get Identity Field Value without Using Stored Procedures PDF Print E-mail
Articles - Visual Basic / .NET Articles
Written by Thomas Kaloyani   
This article describes how to retrieve the identity value when you add a record into a SQL Server table with an auto incrementing identity field.

Getting the value of an identity field when you add a record to database in most of the cases is useful.

One way to accomplish that is to use store procedures. But how you do it if you don't want to use a stored procedure? By using ExecuteScalar method in OleDbCommand or whichever provider you use.

Sample Code:

Dim sSql As String = "INSERT INTO Contents(CustID, FirstName, LastName) VALUES(?,?); SELECT SCOPE_IDENTITY();"

Dim cn As New OleDbConnection(myConnectionString)
Dim cm As New OleDbCommand(sSql, cn)

cm.Parameters.AddWithValue("@FirstName", "FirstName")
cm.Parameters.AddWithValue("@LastName", "LastName")

cm.Connection.Open()
iCustID = cm.ExecuteScalar()
cm.Connection.Close ()

1) In case you use oledb provider you can use named parameters otherwise parameters must be written as a question mark (?) in your sql statement.

2) While you fill the parameters you have to add them in the same order they are written in sql statement.



Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website www.Kaloyani.com or through VBprofs.com - the newest Visual Basic and VB.NET resources portal.

Source: www.VBprofs.com
< Prev   Next >

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer