Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '<' cannot be applied to operands of type 'object' and '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'"

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.

like image 666
HGomez Avatar asked Nov 18 '11 00:11

HGomez


1 Answers

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

 if (Convert.ToInt32(objReturnParam.Value) < 1)
like image 138
Francis Avatar answered Sep 28 '22 06:09

Francis