Question

I have the following Method and both paramaters are null

bool hasValue = HasValue(null,null);

    internal bool HasValue(int? param1, int? param2)
    {
        int count = 0;
        using (var conn = new OracleConnection(connectionString))
        {
                using (var command = conn.CreateCommand())
                {
                    command.CommandText = "select count(id) from  Table1 "
                    + "where ((Column1 = :PARAM1 and :PARAM1 Is Not Null) Or (Column1 Is Null and :PARAM1 Is Null)) "
                    + "AND ((Column2 = :PARAM2 and :PARAM2 Is Not Null) Or (Column2 Is Null and :PARAM2 Is Null))";
                    command.Parameters.Add("PARAM1", OracleDbType.Int16, 0, param1, ParameterDirection.Input);
                    command.Parameters.Add("PARAM2", OracleDbType.Int16, 0,param2, ParameterDirection.Input);
                    command.Connection.Open();
                    count = Convert.ToInt16(command.ExecuteScalar());

                    command.Connection.Close();
                }
        }

        return count > 0;
    }

The method fails with the following error "ORA-01008: not all variables bound"

If I use just one parameter then its fine but when i add the second it fails with "ORA-01008: not all variables bound"

Thanks in advance

Was it helpful?

Solution

 command.CommandText = "select count(id) from  Table1 "
 + "where decode(Column1, :PARAM1, 1) = 1 AND decode(Column2, :PARAM2, 1) = 1";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top