-1

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.

  • I'd strongly recommend re-reading the [mre] guidance on posting code - it would have helped spotting typo of having `value` in quotes (as string) vs. an actual value... Or at very least make problem reproducible (as required by SO guidance) based just on the information in the question. – Alexei Levenkov Apr 27 '23 at 17:04
  • @AlexeiLevenkov I appreciate your feedback. I have updated the code in question to better reflect the community standards. As for the code not able to be reproduced- what else is needed?? – Micah Stovall Apr 27 '23 at 18:22

1 Answers1

1

["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.