I am gonna explaining how to read the data from CSV file using file upload control. A CSV is a comma separated values file, which allows data to be saved in a table structured format. CSVs look like a garden-variety spreadsheet but with a .csv extension (Traditionally they take the form of a text file containing information separated by commas, hence the name). CSV files can be used with any spreadsheet program, such as Microsoft Excel, Open Office Calc, or Google Spreadsheets. They differ from other spreadsheet file types in that you can only have a single sheet in a file, they can not save cell, column, or row styling, and can not save formulas.

Code
<asp:FileUpload ID="fpContacts" runat="server" /> 
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /></p> 

Here we need to add a new folder to the solution, why because, sometimes it will takes the fake file path. it is better to save the file and read data.

In .cs page, write the following code.
protected void btnUpload_Click(object sender, EventArgs e) 

 if (fpContacts.HasFile) 
 { 
     string spath = Server.MapPath("~/upload"); 
     string csv_file_path = spath + "\\" + fpContacts.FileName; 
     fpContacts.SaveAs(csv_file_path); 

     DataTable csvData = GetDataTableFromCSVFile(csv_file_path); 
 } 


public DataTable GetDataTableFromCSVFile(string csv_file_path) 

 DataTable csvData = new DataTable(); 
 try 
 { 
     using (TextFieldParser csvReader = new TextFieldParser(csv_file_path)) 
     { 
         csvReader.SetDelimiters(new string[] { "," }); 
         csvReader.HasFieldsEnclosedInQuotes = true; 
         //read column names 
         string[] colFields = csvReader.ReadFields(); 
         foreach (string column in colFields) 
         { 
             DataColumn datecolumn = new DataColumn(column); 
             datecolumn.AllowDBNull = true; 
             csvData.Columns.Add(datecolumn); 
         } 
         while (!csvReader.EndOfData) 
         { 
             string[] fieldData = csvReader.ReadFields(); 
             ContactEntity CEntity = new ContactEntity(); 
             B2B_BillsData BData = new B2B_BillsData(); 

             CEntity.ContactName = fieldData[0]; 
             CEntity.Email = fieldData[1]; 
             CEntity.Mobile = fieldData[2]; 
             CEntity.GroupId = ddlGroup.SelectedIndex; 
             CEntity.UserId = userid; 
             if (BData.InsertNewContact(CEntity) == true) 
             { 
                 lblMsg.ForeColor = Color.Green; 
                 lblMsg.Text = "Contact Saved successfully"; 
             } 
             csvData.Rows.Add(fieldData); 
         } 
     } 
 } 
 catch (Exception ex) 
 { 
     //MessageBox.Show(ex.Message); 
 } 
 return csvData; 
}

HostForLIFE.eu ASP.NET Core 1.0 Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.