问题描述
我正在ASP.NET和C#中创建用户登录名,但是在编写函数后由于错误而无法编译.错误状态为运算符'<'不能应用于类型为'object'和'int'"
I am creating a user login in ASP.NET and C# however after writing up a function I cannot compile due to an error. The error states "Operator '<' cannot be applied to operands of type 'object' and 'int'"
我要检查ExecuteNonQuery的返回值是否大于0.否则登录应失败.
I want check if the return value from the ExecuteNonQuery is greater than 0. Otherwise the login should fail.
该存储过程是与该类中早先的已确认数据库连接字符串一起创建的.
The stored procedure is created along with a confirmed database connection string earlier on in the class.
login.aspx.cs
public bool DBConnection(string strUserName, string strPassword)
{
SqlCommand myCommand = new SqlCommand("ValidateUser", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter objParam1 = default(SqlParameter);
SqlParameter objParam2 = default(SqlParameter);
SqlParameter objReturnParam = default(SqlParameter);
objParam1 = myCommand.Parameters.Add("@USERNAME", SqlDbType.VarChar);
objParam2 = myCommand.Parameters.Add("@PASSWORD", SqlDbType.VarChar);
objReturnParam = myCommand.Parameters.Add("@NUM_OF_USER", SqlDbType.Int);
objParam1.Direction = ParameterDirection.Input;
objParam2.Direction = ParameterDirection.Input;
objReturnParam.Direction = ParameterDirection.ReturnValue;
objParam1.Value = textUserName.Text;
objParam2.Value = textPassword.Text;
try
{
if (_productConn.State == ConnectionState.Closed)
{
_productConn.Open();
myCommand.ExecuteNonQuery();
}
//// ERROR HERE - I Want to check if the return value is greater than 0 ???
if (objReturnParam.Value < 1)
{
lblMessage.Text = "Invalid Login!";
}
else
{
return true;
}
_productConn.Close();
}
catch (Exception ex)
{
lblMessage.Text = "Error Connecting to Database!";
}
}
任何帮助将不胜感激,因为我对此一无所知.谢谢.
Any help would be much appreciated as I'm lost on this one. Thanks.
推荐答案
您必须强制转换Value,因为它是一个对象
You have to cast the Value because it's an object
if (Convert.ToInt32(objReturnParam.Value) < 1)
这篇关于运算符&#39;&#39;不能应用于类型&#39; object&#39;的操作数和&#39; int&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!