Reputation: 37
I'm a beginner at C#. I can't call a stored procedure.
My stored procedure is this:
CREATE PROCEDURE USP_login
@us VARCHAR(20),
@pwd VARCHAR(20)
AS
BEGIN TRAN
BEGIN TRY
SELECT *
FROM dbo.KhachHang
WHERE tenDangNhap = @us AND matKhau = @pwd
END TRY
BEGIN CATCH
ROLLBACK TRAN
RETURN 0
END CATCH
COMMIT TRAN
RETURN 1
GO
In my C# code, I use this function to call the USP_login
stored procedure but it doesn't work:
public bool loginStored(string us, string pwd)
{
object[] sqlParams =
{
new SqlParameter ("@userName", us),
new SqlParameter ("@passWord", pwd),
};
var rs = db.Database.SqlQuery<bool>("USP_login @userName, @passWord", sqlParams).SingleOrDefault();
return rs;
}
Error message in screenshot:
Upvotes: 0
Views: 121
Reputation: 218798
Looks like SELECT * ...
is returning more than just a single bool
. (Based on the query, clearly the table has at least two fields, tenDangNhap
and matKhau
.) But that's what you told the code to expect:
db.Database.SqlQuery<bool>(/.../)
Either select only the column you want:
SELECT SomeBooleanValue FROM dbo.KhachHang WHERE tenDangNhap=@us AND matKhau=@pwd
Or specify the correct type that can be expected for each record (which may be a custom class that you need to define):
db.Database.SqlQuery<SomeObjectType>(/.../)
Upvotes: 4