stringWrite - Length can't be less than zero

102 Views Asked by At

I'm exporting a datagrid data to Excel and the exporting function worked fine until I fixed the header row, now an error appears in the export function in this line:

sAux = stringWrite.ToString().Substring(0, stringWrite.ToString().IndexOf("<tr"));

It seems stringWrite is empty and so a warning message appears:

System.ArgumentOutOfRangeException: Length can't be less than zero.

1

There are 1 best solutions below

0
On BEST ANSWER

This means that IndexOf() returned -1. IOW, it didn't find <tr. You can't take a SubString(0, -1).

Declare a variable, and assign the result of IndexOf() to that variable, and make sure the substring you want was found:

Idx = stringWrite.ToString().IndexOf("<tr");
if (Idx > -1)
{
   sAux = ...
}