2

So I would only like to assign the value to 'originalCOlumName' if there is a value in dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString(); if it is NULL or out of range, doesnt exist etc...I want to skip this part. Ive tried looking at another example on Preventing Index Out of Range Error

but NULL checkers didnt work also tried

if(string.IsNullOrEmpty(dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString())
Community
  • 1
  • 1
John
  • 3,965
  • 21
  • 77
  • 163
  • you may need to check against DBNull.value in addition to your check – GH Karim Jun 21 '16 at 11:38
  • 3
    If you're getting an index out of range exception you need to check the Length or Count property of `Rows` and then `ItemArray` against the indexes you are using. – juharr Jun 21 '16 at 11:39

1 Answers1

0

You would need to check the length of both the Rows and the ItemArray collections to ensure they have enough elements to index into... Remembering that to get element number 3, the array must contain 4 items since numbering starts at 0. This would look something like;

var rowsLength = dataStore.DataSourceDef.Rows.Count;
if (rowsLength >= columnIndex + 1){
    var itemArrayLength = dataStore.DataSourceDef.Rows[columnIndex].ItemArray.Count;
    if (itemArrayLength >= 4){
        var theString = dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString();
    }
}
Milney
  • 6,253
  • 2
  • 19
  • 33
  • 2
    You can just do `columnIndex < rowsLength`. No need to complicate it by adding 1 and also checking equality. – juharr Jun 21 '16 at 12:08