How to use SCOPE IDENTITY in ASPNET
In this code sample we will take a look at how to use SCOPE_IDENTITY() in ASP.NET. We use this function to get new ID of inserted record from SQL database.
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim strConnection, strSQL As String
Dim newUserID As String
' Database Connection
strConnection = "server=LOCAL;uid=sa;password=;database=sample_database"
' Our SQL String that will insert a new record to database table
strSQL = "Insert into member_list ( "
strSQL + = "userName ,passWord"
strSQL + = ")"
strSQL + = " values ("
strSQL + = "@uName,@pName"
strSQL + = ")"
' Here this part of SQL code will take the latest identity
strSQL + = "; SELECT SCOPE_IDENTITY() ; "
objConnection = New SqlConnection(strConnection)
objCommand = New SqlCommand(strSQL, objConnection)
objCommand.Parameters.Add("@uName", SqlDbType.NVarChar).Value = "some text here or a variable"
objCommand.Parameters.Add("@pWord", SqlDbType.NVarChar).Value = "some text here or a variable"
objCommand.Connection.Open()
newUserID = objCommand.ExecuteScalar
objCommand.Connection.Close()
SCOPE_IDENTITY takes only latest records identity only in current scope, but if you use @@IDENTITY will retrurn identity in all scopes.
Happy Coding
- How to Refresh a web page or redirect to another page
- Custom Paging in ASP Fast And Easy
- Professional Contact Form
- Creating Access Database Connection String
- Using DateDiff function in SQL Statement
- VBScript Examples DatePart Function
- Executing Transact SQL UPDATE statement using SQLCommand
- Custom Paging results in ASPNET using A Better Way
- Form to Mail Using CDO
- Sending Emails in JSP
- MySQL AB Offers its Enterprise Database Subscriptions through the New Red Hat Exchange
- Display Top N records from MS Access
- How to display date time in real time
- JSP Hit Counter Free Open Code
- ASP Ikon Ad Manager UPDATED
- Easy Download Manager Free Version
- ASP Chat Box
- Writing Form Data to Text Files
- Paging Records in JSP
- What is Ecommerce and the Importance of Ecommerce
- Beginning-Ajax-and-ASPNET-35
- How to upload images to mySQL within PHP
- Simple XML RSS Parser
- Counting records in PHP mySQL
- ActiveNews Manager Freeware News Exchange Software
- How to write and use a JavaBean My frst java bean
- Paging Results in PHP
- Free User Registration Form
- A-timesaver-control-GridView
- ASPNET Web Counter Free Open Source Code
- Easy Download Manager Free Version
- ASP Chat Box
- Writing Form Data to Text Files
- Paging Records in JSP
- Beginning-Ajax-and-ASPNET-35
- How to upload images to mySQL within PHP
- Simple XML RSS Parser
- ActiveNews Manager Freeware News Exchange Software
- Counting records in PHP mySQL
- How to write and use a JavaBean My frst java bean
- Paging Results in PHP
- Free User Registration Form
- A-timesaver-control-GridView
- ASPNET Web Counter Free Open Source Code
- Comparing Dates