I am trying to get returned value from stored procedure, but it always returning 0.
cmd = new SqlCommand();
cmd.CommandType = CommandType.S
You do not have an out parameter in your stored procedure, and you are using ExecuteNonQuery -- basically, client side you aren't giving the reurn value anywhere to go. In C# terms it's as if you called a function, but didn't make it part of an assignment. x=getX(); vs getX();
You can fix this by either changing the stored procedure sothat it has an output parameter
ALTER PROCEDURE [dbo].[AbsentEntry]
@EmpID varchar(10),
@AttendanceDate Date,
@returnval int OUTPUT
AS BEGIN
IF (SELECT COUNT(*) FROM tblEmpAttendance
WHERE EmpID = @EmpID AND AttendanceDate=@AttendanceDate) = 0
BEGIN
insert into tblEmpAttendance (EmpID, AttendanceDate, IsInOut,
SessionCount, IsPresent) values ( @EmpID,@AttendanceDate,'OUT',0,'A')
set @returnval=1
END
ELSE
BEGIN
set @returnval=0
END
END
Or, more simply, by using ExecuteScalar and assign the result to a variable.
int result = (int) cmd.ExecuteScalar()