I have a stored procedure that is called to validate a user during login. If success it returns the user entity, and that works good! My question is if it doesn't work, I'll raise an error in the SP, How do I catch this error and use it in the best way? Right now I'm getting nullrefference, this is the code: Store procedure:
ALTER PROCEDURE getEmployee
(
@username nvarchar(50),
@password nvarchar(50)
)
AS
DECLARE @Error_MSG nvarchar(50)
BEGIN
IF EXISTS (select * from Employee where eUsername = @username AND pword = @password)
begin
select * from Employee where eUsername = @username AND pword = @password
END
ELSE
BEGIN
SET @Error_MSG = 'Wrong password, or user doesnt exist'
RAISERROR (@Error_MSG, 11,1)
END
END
And in the code it looks like this, the SP is getEmployee
ActivityDatabaseDataContext dc = new ActivityDatabaseDataContext();
Employee emp;
public bool logIn(string piUsername, string piPassword)
{
try
{
emp = dc.getEmployee(piUsername, piPassword).Single();
}
catch (Exception ex)
{
errorMsg = ex.Message + ex.InnerException.Message;
}
if (emp != null)
{
AppHelper.AppHelper.setUser(emp);
return true;
}
else
{
return false;
}
My question is how I should handle the exception?