[英]Application generating Error:Must Declare Scalar Variable In Web Api 2. Also, practices regarding the code in WebApi2 and debugging Controller method
I asked a lot in here, as I am new to MVC, WebApi and RestService. 我在这里问了很多问题,因为我是MVC,WebApi和RestService的新手。
So, I am working with an application where StoredProcedure is being called inside Controller, but getting Error : 因此,我正在使用其中在Controller内调用StoredProcedure的应用程序,但出现错误:
Must Declare Scalar Variable @empid
必须声明标量变量@empid
Code is below : 代码如下:
[HttpGet]
[Route("~/api/Login/{EmpID}/{pwd}")]
public IHttpActionResult ValidateLoginDetails(string EmpID, string pwd)
{
int a=0;
if (EmpID == null)
EmpID = "";
else a = Int32.Parse(EmpID);
if (pwd == null)
pwd = "";
var val = new SqlParameter()
{ParameterName="@val",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int};
var e = new SqlParameter("@empid", SqlDbType.Int) ;
e.Value = a;
//User.Identity.Name;
var y = new SqlParameter("@pwd", SqlDbType.VarChar);
y.Value = pwd;
using (var x = new DB_BgCheckEntities())
{
var retVal = x.Database.SqlQuery<IEnumerable<int>>
("Exec proc_Bgcheck_login @empid,@pwd,@val out", e, y, val);
// var ret = retVal.Single();
//foreach(var ab in retVal)
//{
// Console.WriteLine(ab.ToList());
//}
//retVal.ToList();
x.Database.ExecuteSqlCommand("Exec proc_Bgcheck_login @eid,@pwd,@val out", e, y, val);
if ((int)val.Value == 1)
{
return Ok(1);
}
else return NotFound();
}
}
Below statement is doing my work, but I am not getting why the dbcontext is not working 下面的语句正在做我的工作,但我不明白为什么dbcontext不起作用
x.Database.ExecuteSqlCommand("Exec proc_Bgcheck_login @eid,@pwd,@val out", e, y, val);
Stored Proc : 存储过程:
CREATE PROCEDURE proc_Bgcheck_login
(
@emp_no int,
@pwd varchar(255),
@val int output
)as
BEGIN
SET @val=0;
DECLARE @count int;
SELECT @count=COUNT(*) FROM tbl_User_Login where [Emp No]=@emp_no and [Password]=@pwd;
IF (@count = 1)
BEGIN
SET @val=1;
RETURN @val;
END
ELSE
RETURN @val;
END
GO
Few questions, I need to ask : 几个问题,我需要问:
PS : For the error, I went through different sites and threads of StackOverflow, but none resolved my solution.
PS:对于错误,我经历了StackOverflow的不同站点和线程,但没有一个解决我的解决方案。
The error : 错误 :
Must declare Scalar Variable @empid is resolved .
必须声明标量变量@empid已解决。
As mentioned by @amit, I haven't passed the parameters for the @empid and @pwd . 如@amit所述,我尚未传递@empid和@pwd的参数。
So, the correct way to pass the parameter was : 因此,传递参数的正确方法是:
var retVal = dbcontext.Database.SqlQuery<tbl_User_Login>
("Exec proc_Bgcheck_login @empid,@pwd,@val ",e,y, val).ToList();
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.