title: C# Index Out Of Range Exception when creating date
body: <p>I have written a piece of code where it connects to SQL Server to retrieve some data from the database. One type of these data that I need is a stored date for something. This works fine, it connects and retrieves all the data I want from it, but when I try to create a <code>DateTime</code> using the retrieved date I've got from SQL it gives me a Index Out of Range Exception.</p>
  
  <p>Here is the code for it:</p>
  
  <pre><code>//sDate contains 14/11/2014 so dateArr[0] contains 14, dateArr[1] contains 11 and dateArr[2] contains 2014
  
  for(int i = 0; i &lt; dataSet.Tables[0].Rows.Count; i++)
  {
      if(dataSet.Tables[0].Rows[i][9] != DBNull.Value)
      {
          String[] sDateAndTime = dataSet.Tables[0].Rows[i][9].ToString().Split(' ');
          String sDate = sDateAndTime[0];
          String[] dateArr = sDate.Split('/');
          DateTime date = new DateTime(Convert.ToInt32(dateArr[3]), Convert.ToInt32(dateArr[2]), Convert.ToInt32(dateArr[1]));
  
          Console.WriteLine(dateArr[0] + " " + dateArr[1] + " " + dateArr[2]);
      }
  }
  </code></pre>
  
  <p>The strange thing is that the error only comes up on line <code>DateTime date = new DateTime(Convert.ToInt32(dateArr[3]), Convert.ToInt32(dateArr[2]), Convert.ToInt32(dateArr[1]));</code></p>
  
  <p>If I comment that line out, it works totally fine. Can someone please let me know what is it that I'm doing wrong? I've tried all kinds of methods including using <code>DateTime.ParseExact</code> but it still doesn't work (give a different error).</p>