Home > Linq > Get Last Inserted Identity with Stored Procedure using Linq

Get Last Inserted Identity with Stored Procedure using Linq


Hi

try this example :-

let we have a stored procedure which will return the last inserted Record

1) Create new Project and add Linq to SQL  Classes and name it DataContext.dbml

2) Add your table and your Stored Procedure inside DataContext.dbml

Create PROCEDURE [proc_CountriesInsert]
(
 @CountryID smallint = NULL output,
 @CountryName nvarchar(50) = NULL,
 @xCurrency_ID smallint = NULL
)
AS
BEGIN
 
 INSERT
 INTO [Countries]
 (
  [CountryName],
  [xCurrency_ID]
 )
 VALUES
 (
  @CountryName,
  @xCurrency_ID
 )
 SELECT @CountryID = SCOPE_IDENTITY()
END

in code in button click :-

short ? CountryId=0;
DataContext.proc_CountriesInsert(ref CountryId,"xx",1);
 if (countryid.HasValue)
    {
        Label1.Text = "Inserted Value --> " + Convert.ToString(CountryId);
    }
    else
     {
       Label1.Text = "No Value ..";
     }

Hope this helps

Good Luck

Advertisements
Categories: Linq
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: