I have two table name sale info and sale transaction. For that I used foreign key in sale transaction for that I write following stored procedure.
Stored Procedure 1
ALTER PROCEDURE dbo.saleSP
@invoice_no varchar(50),////////////primary key of sale info table
@sale_description text,
@transtype varchar(50),
@ID int output///////////////////////////forign key which are used for the relationship between sale info and sale transaction
AS
insert into sale_info
(invoice_no,sale_description, transtype)
values
(@invoice_no ,@sale_description ,@transtype)
SELECT @ID= SCOPE_IDENTITY();
Stored Procedure 2
ALTER PROCEDURE dbo.ptransactionSP
@product_name varchar(50),
@quantity int,
@price decimal(18, 2),
@total decimal(18, 0),
@ID int /////////////////primary key of sale transction tables
AS
insert into sale_transaction
(total,product_name,quantity,price,ID)
values
(@total ,@product_name ,@quantity ,@price,@ID)
RETURN
The transaction fine. I am working with edmx I have no idea to how I used SCOPE_IDENTITY in C# code.
Following I code which are below.
saleInfo comm = new saleInfo () //////////this is table name
{
string invoiceno =txtinvoice.Text;
string sale_description = txtdecript.Text;
string transtype = txttanstype.Text;
int ID=int.Parse( myCommand.ExecuteScalar());///////Scope Identity
}
Helper.insert(com)
The other definition in the helper
Which are below
public static void Insert(saleInfo insert)
{
Db. saleSP (////////////////this is my stored procedure name
insert. invoiceno,
insert. sale_description,
insert. transtype
);
Every thing is ok but have problem how I get scope identity value in that code and how I used save this value into other table as a forign key please help me to solve that.