Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to execute a stored procedure

SQL
CREATE PROCEDURE [dbo].[sp_Insert_Donor_Details] 
	-- Add the parameters for the stored procedure here
	@Name nvarchar(50),
	@Address nvarchar(MAX),
	@Phone nchar(10),
	@Pin nchar(10),
	@Email nvarchar(50),
	@DOB nvarchar(9),
	@Ann nvarchar(9),
	@DeathAnn nvarchar(9),
	@Family bit
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	INSERT INTO tbl_Donor_Details (Name, [Address], Phone, EmailID, Pincode, DOB, Anniversary, DeathAnniversary, HasFamily)
	VALUES (@Name, @Address, @Phone, @Email, @Pin, @DOB, @Ann, @DeathAnn, @Family)

	SELECT SCOPE_IDENTITY() AS DonorID
END


GO


Here I want to get the DonorID in my code. My c# code to call the stored procedure is:

C#
if (txtName_1.Text != "" || txtName_2.Text != "" || txtName_3.Text != "" || txtName_4.Text != "")
                    family = true;
                var result = GDB.sp_Insert_Donor_Details(txtName.Text, txtAddress.Text, txtPhone.Text,
                    txtEmail.Text, txtPin.Text, txtDOB.Text, txtAnniversary.Text, txtDeathAnniversary.Text, 
                    family).ToList();


Please help me out how can I get result as DonorID (scope identity from the database). Currently I am getting System.Decimal as the value of result.
Posted
Comments
Richard MacCutchan 19-Jan-16 13:06pm    
You do realise that you will execute the stored procedure if any of those text fields contain data. Are you sure that is correct?
Richard Deeming 19-Jan-16 13:26pm    
It will execute the stored procedure regardless of whether the fields contain data. The if block is just updating the family parameter.

1 solution

Try something like this:
C#
int donorID = GDB.sp_Insert_Donor_Details(...)
    .Select(r => r.DonorID)
    .FirstOrDefault();
 
Share this answer
 
Comments
prapti.n3 20-Jan-16 2:06am    
It helped me. Thanks. But insteasd of r.DonorID, I had used r.Value
prapti.n3 21-Jan-16 5:05am    
What to do if the return value is a guid?
Richard Deeming 21-Jan-16 8:02am    
Create the Guid in your C# code (Guid.NewGuid()) and pass it into the stored procedure.

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