David Smith

David Smith

  • NA
  • 2k
  • 0

Datatable Index and ColumnName Validation

Dec 15 2014 4:27 AM
 Can someone assist me with the logic to validate the rowindex is in range and also valid the columnName? How would I do that with the logic below.
 The task is to create my own custom exception derived from ApplicationException. However for this thread I am looking for the robust logic to valid index and column name.
 
public static int GetIntValue(this DataTable datatable, int rowindex, string columnName)
{
      try
      {
         //if index is out bounds throw out of bounds;
         // return what is asking for
         if (datatable != null)
         {
            if (rowindex > -1 && rowindex < datatable.Rows.Count - 1)
            {
               return Convert.ToInt32(datatable.Rows[rowindex][columnName]);
            }
         else
         {
            //return custom exception
         }
      }
      else
      {
         //return custom exception
       }
}
catch (Exception ex)
{
throw new Exception("GetIntValue: \n" + ex.Message);
}
}

Answers (3)