I am trying to select rows to delete from a data table using the dt.Select(query) method. However, the system throws out error message “Cannot perform ‘=’ operation on System.Double and System.String”.
Any idea how to overcome this issue?
I have assigned {“B012345”, “B012346”} into a String array
When I loop through each code inside the String array, I assigned strQuery = String.Format(“Agent=‘{0}’”, code)
When trying to select rows using rowsToDelete = dt.Select(strQuery), I got the above error message.
The “Agent” data column in my Excel consist of both numeric only and alphanumeric codes.
Thanks in advance for any response to my question.
List<DataRow> deletedRows = new List<DataRow>(); --- Create a DataRow List
foreach (DataRow dataRow in dataTable.Rows) // Add rows to List
{
if(dataRow(0)='B012345')
deletedRows.Add(dataRow);
}
foreach(DataRow dataRow in deletedRows) // Delete list
{
dataRow.Delete();
}
Generated DataTable has types set for all columns as Object, but individual cell types are determined by what type Excel assigned.
This means that a value of “B012345” will be stored as a String, but “12345” as a number (so Double).
You can check it by iterating and checking the Type of each.
Yes, I have changed to the same method as your suggestion and it’s working.
However, I need to save those deleted rows into another Excel file for audit purpose. Is there a way to directly output “deletedRows” datarow into an Excel file the same way we did with datatable?
I tried to perform add data row prior to delete row as per your suggestion. However, I got another error saying “This row already belongs to another table.” when it is trying to execute Delete datarow.
foreach (DataRow dataRow in dataTable.Rows) // Add rows to List
{
if(dataRow(0)=‘B012345’) PLEASE ADD HERE AND CHECK IF IT WORKS Add Data Row to new datatable here
deletedRows.Add(dataRow);
}
foreach(DataRow dataRow in deletedRows) // Delete list
{
This is an interesting problem. Im also encountering this when my source data excel has both Strings and Numbers. Is there a way to force the read range to treat the value as a string? I am using the .ToString method on the datatable cell.