Cannot Perform '-' operation on System.String and System.Decimal on DataTable.Select statement

153 Views Asked by At

I am trying to execute this select statement within my application and encounter the System.Data.EvaluateException "Cannot perform '-' operation on System.String and System.Decimal"

foreach (DataRow dataRow in dt.Rows)
    
    string otherSTR = "some values";
    .. stuff happens
    
    double newR = Convert.ToDouble(dataRow["NEW_RATE"]);
    dataRow = dataTable.Select("column = '" + otherSTR + "' and (([double_value] - " + newR + ")*-1  < 0.5) 
    and (([double_value] - " + newR + ")*1  < 0.5)", "Date asc");

    ... more stuff happens

}

The ["NEW_RATE"] column is stored as a String and the ["double_value"] column is stored as a String. Is there something within the query that I may be overlooking? The row that ["NEW_RATE"] is sourced from is a separate table than the dataTable that the SELECT statement executes in.

The ["NEW_RATE"] column is a "money" type within the database and is received in the form of a String to the application.

When receiving the ["NEW_RATE"] column as a double, I had the newR assign statement as follows: double newR = dataRow["NEW_RATE"]; This throws the "Cannot implicitly convert type 'object' to 'double'" error, so I've added the Convert.ToDouble() method while receiving the ["NEW_RATE"] column as a String, and the "object can not be assigned" error goes away but the "Cannot perform '-' operation" issue still persists.

The response from the SELECT statement should provide the rows within the dataTable which align with the conditional "and" statement, sorted in ascending order of the Date column.

1

There are 1 best solutions below

0
Micah Stovall On

["double_value"] was defined as a String instead of a Double within it's row model declaration. There was no problem converting the ["NEW_RATE"] value to a Double from a string. I understood the error backwards.

Was public string double_value { get; set; }

Now public double double_value { get; set; }

Example of the row model below:

public class DataRow
{
    ...
    public double double_value { get; set; }
    ...
}

Should have looked further into this before asking a question.