Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_Registration] (
@CustomerId int,
@EmailId varchar(50),
@FirstName varchar(50),
@Lastname varchar(50),
@Phone1 varchar(15),
@Phone2 varchar(15),
@CompanyName varchar(50),
@Address varchar(max),
@ZipCode varchar(20),
@City varchar(50),
@State varchar(50),
@Country varchar(50),
@TransactionDate datetime,
@Status varchar(30)
)
AS
BEGIN
insert into dbo.CustomerDetails values(@EmailId,@FirstName,@Lastname,@Phone1,@Phone2,@CompanyName
,@Address,@ZipCode,@City,@State,@Country,@TransactionDate,@Status)
Declare @NewId as int
select @NewId= @@IDENTITY
END
In above table customerId is a primary key and auto generated. After inserting this information i need last record i mean recently added record Customer Id Please need help
Posted
Updated 21-Aug-12 21:59pm
v2

Just make the following changes in stored proc.

Add select @@identity

Instead of this,
SQL
Declare @NewId as int
select @NewId= @@IDENTITY
END


now ur code will look like
SQL
Select @@Identity
END


Now in aspx page when executing command.
C#
SqlCommand cmd = new SqlCommand();
CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
int ID=(Int32)cmd.ExecuteScalar();
sqlConnection1.Close();
 
Share this answer
 
Comments
__TR__ 22-Aug-12 4:30am    
My 5!
If your Id is in auto-increment then retrieve MAX(ID) after inserting the data.
 
Share this answer
 
use return keyword,
SQL
Declare @NewId as int
select @NewId= @@IDENTITY
return @NewId

Happy Coding!
:)
 
Share this answer
 
Comments
Lakshmimsridhar 22-Aug-12 4:13am    
how in aspx.cs page??
Hi,
Change the last line in your stored procedure to
SELECT @@Identity
and use ExecuteScalar() method in your code behind to get the ID.

Below link should give you some idea
SqlCommand.ExecuteScalar Method[^]
 
Share this answer
 
Comments
Lakshmimsridhar 22-Aug-12 6:28am    
public int InsertRegistrationDetails()
{
SqlCommand cmd = new SqlCommand("sp_Registration",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerId", CustomerId);
cmd.Parameters.Add("@EmailId",EmailId);
cmd.Parameters.Add("@FirstName",FirstName);
cmd.Parameters.Add("@Lastname",Lastname);
cmd.Parameters.Add("@Phone1",Phone1);
cmd.Parameters.Add("@Phone2",Phone2);
cmd.Parameters.Add("@CompanyName",CompanyName);
cmd.Parameters.Add("@Address",Address);
cmd.Parameters.Add("@ZipCode",ZipCode);
cmd.Parameters.Add("@City",City);
cmd.Parameters.Add("@State",State);
cmd.Parameters.Add("@Country",Country);
cmd.Parameters.Add("@TransactionDate",TransactionDate);
cmd.Parameters.Add("@Status",Status);
cmd.ExecuteNonQuery();
int NewId = 0;
NewId = Convert.ToInt32(cmd.ExecuteScalar().ToString());
return NewId;
}
this is a class. this function returns scalar value. how to take that value in code behind and assign this return value to customer id
__TR__ 22-Aug-12 8:18am    
Since the method returns an integer, just call the method and assign it to an int variable.
something like:
int CustomerID = InsertRegistrationDetails();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900