title: Dynamic DataTable Creation in ASP.NET
body: <p>I have membership database which contains Roles defined in the role table..Now i want to make datatable to be created dynamically from the the number of roles present in roles table...</p>
  
  <p>here is my code..</p>
  
  <pre><code>DataTable dTable = new DataTable();
  
  string[] rolesarr = Roles.GetAllRoles();
  int length = rolesarr.Count();
  
  for (int i = 0; i &lt;= length; i++)
  {
       string colname = rolesarr[i];
       if (i == 0)
       {
           dTable.Columns.Add(colname, typeof(string));
       }
       else
       {
           dTable.Columns.Add(colname, typeof(bool));
       }
  }
  </code></pre>
  
  <p>but it is giving error as </p>
  
  <blockquote>
    <p><em>"System.IndexOutOfRangeException: Index was outside the bounds of the array."</em></p>
  </blockquote>
  
  <p>Any help will be highly appreciated.
  Thanks in advance..</p>