6

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.

HGomez
  • 1,494
  • 8
  • 32
  • 42

3 Answers3

24

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

 if (Convert.ToInt32(objReturnParam.Value) < 1)
Francis
  • 3,335
  • 20
  • 46
4

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)
sq33G
  • 3,320
  • 1
  • 23
  • 38
-1

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

int ReturnID = 0;
int.TryParse(objReturnParam.Value, ReturnID);
if(ReturnID > 0)
{
      .............
}
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
  • That won't work unless objReturnParam.Value is a string property, but we know it's an object. – phoog Nov 18 '11 at 01:02
  • The Value property of a SqlParameter is a string. If this conversion doesn't work, then ReturnID will be zero, thus meeting the requirements of the if statement. – TheGeekYouNeed Nov 18 '11 at 01:06
  • Because ReturnID is not defined as nullable (int?), on a conversion failure, the value is 0. – TheGeekYouNeed Nov 18 '11 at 01:07
  • Both the OP's question and the documentation ( http://msdn.microsoft.com/en-us/library/ty9hx077.aspx ) indicate that the type of the property is object, not string. I did not vote down, by the way. – phoog Nov 18 '11 at 01:15
  • @TheGeekYouNeed that code will not even compile, the Value property of a SqlParameter is an object – Ben Robinson Nov 18 '11 at 10:01