Return value from Stored Procedure

  • Thread starter Thread starter Shayaan
  • Start date Start date
S

Shayaan

Guest
The following code always return False even though it finds a record, what am I doing wrong?

protected bool GetUID(string sUserID)
{
bool exists;

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("CheckExistingID", con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
cmd.Parameters.Add(new SqlParameter("@given_id", sUID));
param.Direction = ParameterDirection.Input;
cmd.Connection = con;
con.Open();
var result = cmd.ExecuteScalar();

exists = result != null ? (int)result > 0 : false;
using (SqlDataAdapter sda = new SqlDataAdapter())
con.Close();
}
}
if (exists)
{
return true;
}
else
{
return false;
}
}


********************* Stored Procedure*********************

ALTER PROCEDURE [dbo].[CheckExistingEntityTest]
(
@given_id as varchar(9)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Exists INT

IF EXISTS(
SELECT top 1 FirstName, LastName from Users
WHERE UserID = @given_id
)
BEGIN
SET @Exists = 1
END
ELSE
BEGIN
SET @Exists = 0
END

RETURN @Exists
END

Continue reading...
 
Back
Top