Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
ALTER PROCEDURE [dbo].[AddRegistration1Detail]
(
	@FirstName varchar(50),
	@LastName varchar(50)=null,
	

)
AS
	SET NOCOUNT OFF;
INSERT INTO [Registration1] ([FirstName], [LastName]) VALUES (@FirstName, @LastName);

select @@IDENTITY as newid


and my class is

C#
public static int AddRegistration1Detail(string FirstName, string LastName )
   {
       comm = new SqlCommand("AddRegistration1Detail", conn);
       comm.CommandType = CommandType.StoredProcedure;
       comm.Parameters.AddWithValue("@FirstName", FirstName);
       comm.Parameters.AddWithValue("@LastName", LastName);
       RegistrationDate);
       comm.Parameters.AddWithValue("@IsActive", IsActive);

       return Convert.ToInt32(CommExecuteNonQuery(comm));


   }

how to return identity?
Posted
Updated 16-Jul-12 10:12am
v2

Hi ,
Check this Example will guide you
SQL
create proc  usp_test_Insert
(@OrderAmount int , @date datetime)
as
begin
insert into dbo.Orders
(  OrderAmount, [date])
values
(@OrderAmount ,@date)

select  SCOPE_IDENTITY()
end


C#
protected void Button1_Click(object sender, EventArgs e)
 {
     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
     {
         con.Open();
         using (SqlCommand cmd = new SqlCommand("usp_test_Insert",con))
         {
             cmd.CommandType = System.Data.CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@OrderAmount",TextBox1.Text);
             cmd.Parameters.AddWithValue("@date", DateTime.Now);
              int ID = Convert.ToInt32(  cmd.ExecuteScalar());
              Response.Write("<script>alert('"+ID.ToString()+"')</script>");
         }
     }
 }

Best Regards
M.Mitwalli
 
Share this answer
 
v2
Your stored procedure can return a value using the OUTPUT keyword. This can be retrieved in asp.net using another parameter, whose Direction property is set to ParameterDirection.Output.

Refer to this article from 4guys in rolla, which explains this in detail:

http://www.4guysfromrolla.com/articles/062905-1.aspx[^]
 
Share this answer
 
Comments
krupalmehta 16-Jul-12 16:17pm    
i read this one early.. but i cant develope it. so please give me code for return identity
also ,you can use output param.
this
 
Share this answer
 

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