运算符 '<'不能应用于“object”类型的操作数;和“int”

发布于 2024-12-16 19:34:13 字数 1959 浏览 0 评论 0 原文

我正在 ASP.NET 和 C# 中创建用户登录,但是在编写函数后,由于错误而无法编译。错误指出“运算符 '<'不能应用于类型为“object”和“int”的操作数”

我想检查 ExecuteNonQuery 的返回值是否大于 0。否则登录会失败。

该存储过程是在类的前面与已确认的数据库连接字符串一起创建的。

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!";
            }
        }

任何帮助将不胜感激,因为我对此一无所知。谢谢。

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'"

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-12-23 19:34:13

你必须强制转换 Value 因为它是一个对象

 if (Convert.ToInt32(objReturnParam.Value) < 1)

You have to cast the Value because it's an object

 if (Convert.ToInt32(objReturnParam.Value) < 1)
若水微香 2024-12-23 19:34:13

如前所述,值是对象类型。如果您确信它始终包含 int (而不是 DBNull.Value),则可以在比较之前将其强制转换为 int:

if ((int)objReturnParam.Value < 1)

Value is, as stated, of type object. If you are confident that it always contains an int (and not, for example, DBNull.Value), you can cast it to int before the comparison:

if ((int)objReturnParam.Value < 1)
∞梦里开花 2024-12-23 19:34:13

如果 objReturnParam 为 null,则应使用 TryParse。

int ReturnID = 0;
int.TryParse(objReturnParam.Value, ReturnID);
if(ReturnID > 0)
{
      .............
}

In case of objReturnParam being null, you should use TryParse.

int ReturnID = 0;
int.TryParse(objReturnParam.Value, ReturnID);
if(ReturnID > 0)
{
      .............
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文