I am using Linq 2 Sql in VS 2010, and I have the following stored procedure to check a username and password
ALTER PROCEDURE dbo.CheckUser ( @username varchar(50), @password varchar(50) ) AS SELECT * FROM Users Where UserName=@username AND Password=@password
The problem I’m having is that it throws an exception if the username and password are incorrect. I’d like to perform a check to see if there is a return value, rather than using try/catch to determine whether the procedure returned a value.
Should I do this check in code (C#)? Or is there a way to do it in SQL?
Thanks.
Edit:
Code throwing the exception:
MyDatabaseDataContext db = new MyDatabaseDataContext();
String username = "username";
String password = "password";
User u = db.CheckUser(username, password); // Exception
It looks like you are only interested to know if your username and password combination exists (from the name of the stored procedure).
So you should probably do
Which will just return the count of records with that combination. Then in code, you can check for the scalar returned by the stored procedure and if it’s 0, then there is no such user.