find a row (s) with DataTable.Select

1

I carry out the following instruction ..

 DataRow[] results = m_dtableKardex.Select("IdReferencia = "                 + l_intIdReferencia         + " OR IdReferencia IS NULL" +
                                                   " AND IdTamano = "               + l_intIdTamano             + " OR IdTamano IS NULL" +
                                                   " AND IdLamina = "               + l_intIdLamina             + " OR IdLamina IS NULL" +
                                                   " AND IdColorLamina = "          + l_intIdColorLamina        + " OR IdColorLamina IS NULL" +
                                                   " AND IdAcabado = "              + l_intIdAcabadoHerraje     + " OR IdAcabado IS NULL" +
                                                   " AND IdColorRecubrimiento = "   + l_intIdColorRecubrimiento + " OR IdColorRecubrimiento IS NULL" +
                                                   " AND IdColorSuperficial ="      + l_intIdColorSuperficial   + " OR IdColorSuperficial IS NULL");

where any of the conditions can be null, but it shows me the following error:

  

System.NullReferenceException was not controlled     Message = Reference to object not set as an instance of an object.

if only I send a valid data so that it shows the result of the DataRow, with the OR IS NULL Would it obviate the other variables ??

    
asked by ger 08.02.2018 в 20:41
source

1 answer

2

The code you share has a problem: operator precedence is not taken into account. In almost all languages the operator Y has more precedence than the operator O (because Y is more restrictive).

That means that although in your code you have it sorted as:

  a O b
Y c O d 
Y e O f

This graphic representation does not correspond to the interpretation that is going to be made of the conditions. Because having O less precedence than Y , then what is really being done is:

  a 
O (b Y c) 
O (d Y e) 
O f

The graphic representation that you make of the condition does not match its interpretation that is going to be done by the language, and that would make it possible to escape a variable with NULL value that causes the error you receive.

The solution would be to use parentheses to correctly separate the operations, because what you seem to want to do is:

  (a O b) 
Y (c O d) 
Y (e O f)

That applied to your code would look like this:

 DataRow[] results = m_dtableKardex.Select("(IdReferencia = "           + l_intIdReferencia         + " OR IdReferencia IS NULL)" +
                                      " AND (IdTamano = "               + l_intIdTamano             + " OR IdTamano IS NULL)" +
                                      " AND (IdLamina = "               + l_intIdLamina             + " OR IdLamina IS NULL)" +
                                      " AND (IdColorLamina = "          + l_intIdColorLamina        + " OR IdColorLamina IS NULL) " +
                                      " AND (IdAcabado = "              + l_intIdAcabadoHerraje     + " OR IdAcabado IS NULL) " +
                                      " AND (IdColorRecubrimiento = "   + l_intIdColorRecubrimiento + " OR IdColorRecubrimiento IS NULL)" +
                                      " AND (IdColorSuperficial = "     + l_intIdColorSuperficial   + " OR IdColorSuperficial IS NULL)");
    
answered by 08.02.2018 / 21:04
source