Retrieving ID into textbox after inserting a particular row in sql server

Posted by Nishadullas under C# on 12/3/2011 | Points: 10 | Views : 1718 | Status : [Member] | Replies : 3
Hi..

I am trying to insert a values into a row in Sql server 's table, for that i have used stored procedure and its working fine.
Now i want to retrieve the unique ID i.e.("Emp_ID" its the primary key which is set to identity,auto increment) of table in which i am inserting values,
My requirement is to get that newly generated id of the row in which value is recently inserted. By searching this in google i got a concept known as "Output parameter " in stored procedure.
But i didn't get the entire information on this,like how to set this and how to retrieve that value into a textbox in windows application just immediate after inserting the values in table.

So if anyone can help me to get clear with this concept it would be very helpfull for me and if possible try to get any demo or links so that i can refer that.

Thank you.


regards
Nishad




Responses

Posted by: Ravindra.patil on: 12/3/2011 [Member] Starter | Points: 25

Up
0
Down
HI Nishad Panattil's ,

try below Stored procedure may help you.


Create proc [dbo].[usp_InsertINTOab]
(
@EmpName varchar(50),
@EmpLastName varchar(50),
@ReturnValue int out
)
as
begin

insert into ab(EmpName,EmpLastName)values(@EmpName,@EmpLastName)

SET @ReturnValue=SCOPE_IDENTITY();
end


Nishadullas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishadullas on: 12/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Ravindra,
Thank you for replying.
I tried the above code you have provided and its done, it is retrieving the recently enetred row's id.
But can you just tell me further that how can i grab that "@ReturnValue" in a textbox.


Regards
Nishad


Nishadullas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ravindra.patil on: 12/3/2011 [Member] Starter | Points: 25

Up
0
Down
HI Nishad Panattil's,

if your using out parameter means you have to use ref keyword the below code display Returns @returnvalue in textbox
i have used layers in my page classdomain is my domain layer dm.insert is my insert method

string FName = "xxx";
string LName = "YYY";
Class1Domain dm1 = new Class1Domain();
int? ReturnValue =0 ;
dm1.Insert(FName,LName,ref ReturnValue);

txtReturn.Text =Convert.ToString(ReturnValue);


Nishadullas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response