Make an Output parameter in Stored Procedure and Get it By Code In C# -:
First -Create Stored Procedure (SP) in MS SQL SErver as-
create PROCEDURE DeletePayrollSetup
(
@Category nvarchar(20),
@Head nvarchar(20),
@NewHead nvarchar(50),
@val int output
)
AS
declare @chk int
set @chk = 0
if exists(select * from salarycal where category = @Category and formula like @NewHead )
begin
set @chk = 2
end
else if exists(select * from PayrollConditionallycalc where category = @Category and mainhead like @NewHead or ans like @NewHead)
begin
set @chk = 2
end
else
begin
set @chk = 1
end
set @val = @chk
Now use this Sp in Our Code as-
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[((MasterForm)(this.ParentForm)).CnnStr].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("DeletePayrollSetup");
cmd.Parameters.AddWithValue("@Category", strCatId);
cmd.Parameters.AddWithValue("@Head", strHead);
cmd.Parameters.AddWithValue("@NewHead", strHeadIdNew);
cmd.Parameters.Add(new SqlParameter("@val", SqlDbType.Int));
cmd.Parameters["@val"].Direction = ParameterDirection.Output;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
int Val = Convert.ToInt32(cmd.Parameters["@val"].Value);
con.Close();
This will hold the @val by giving its Direction
using this line-
cmd.Parameters["@val"].Direction = ParameterDirection.Output;
and
Then we hold a output value as-
int Val = Convert.ToInt32(cmd.Parameters["@val"].Value);
waw! its strange..
ReplyDelete