title: An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll : Cannot find column 3
body: <p>I have try this code to import data to SQL table but it is showing up the error:
  This error occurs when I upload an Excel file, but when I upload Notepad file the information  imported to SQL table.</p>
  <p>the place of error:</p>
  <pre><code>dt.Rows[dt.Rows.Count - 1][i] = cell;
  </code></pre>
  <p>So Please can anyone try to figure out the error and can send me the correct code</p>
  <pre><code>protected void Upload(object sender, EventArgs e)
  {        
    string csvPath = Server.MapPath(&quot;~/Files/&quot;) + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(csvPath);
  
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&quot;PersonId&quot;, typeof(string)),
          new DataColumn(&quot;Name&quot;, typeof(string)), new DataColumn(&quot;Salary&quot;,typeof(string))});
          
    string csvData = File.ReadAllText(csvPath);
    foreach (string row in csvData.Split('\n'))
    {
        if (!string.IsNullOrEmpty(row))
        {
            dt.Rows.Add();
            int i = 0;
            foreach (string cell in row.Split(','))
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell;
                i++;
             }
        }
     }
  
          using (SqlConnection con = new SqlConnection(&quot;Data Source=Server Name;Initial Catalog=AD;Integrated Security=True&quot;))
          {
              using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
              {
                  //Set the database table name.
                  sqlBulkCopy.DestinationTableName = &quot;dbo.Employee&quot;;
                  con.Open();
                  sqlBulkCopy.WriteToServer(dt);
                  con.Close();
              }
          }
      }
  }
  }        
  </code></pre>