title: C# VS2005 Import .CSV File into SQL Database
body: <p>I am trying to import a .csv file into my database. I am able to import an excel worksheet into my database, however due to different file format as .csv as from .xls, I need to make an import function specially for .csv.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
      if (FileUpload1.HasFile)
      {
          // Get the name of the Excel spreadsheet to upload. 
          string strFileName = Server.HtmlEncode(FileUpload1.FileName);
  
          // Get the extension of the Excel spreadsheet. 
          string strExtension = Path.GetExtension(strFileName);
  
          // Validate the file extension. 
          if (strExtension != ".xls" &amp;&amp; strExtension != ".xlsx" &amp;&amp; strExtension != ".csv" &amp;&amp; strExtension != ".csv")
          {
              Response.Write("&lt;script&gt;alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');&lt;/script&gt;");
              return;
          }
  
                      // Generate the file name to save. 
              string strUploadFileName = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
  
              // Save the Excel spreadsheet on server. 
              FileUpload1.SaveAs(strUploadFileName);
  
              // Create Connection to Excel Workbook
              string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Text;";
              using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)){
              OleDbCommand ExcelCommand = new OleDbCommand("SELECT [columns] FROM +userrolelist", ExcelConnection);
  
              OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
  
              ExcelConnection.Open();
  
          using (DbDataReader dr = ExcelCommand.ExecuteReader())
          {
              // SQL Server Connection String
              string sqlConnectionString = "Data Source=&lt;IP&gt;;Initial Catalog=&lt;DB&gt;;User ID=&lt;userid&gt;;Password=&lt;password&gt;";
  
              // Bulk Copy to SQL Server
              using (SqlBulkCopy bulkCopy =
                         new SqlBulkCopy(sqlConnectionString))
              {
                  bulkCopy.DestinationTableName = "DEMUserRoles";
                  bulkCopy.WriteToServer(dr);
                  Response.Write("&lt;script&gt;alert('DEM User Data imported');&lt;/script&gt;");
  
              }
          }
          }
      }
      else Response.Write("&lt;script&gt;alert('Failed to import DEM User Roles Data. Cause: No file found.');&lt;/script&gt;");
  }
  </code></pre>
  
  <p>The file has been successfully saved, but the error says that the path for the file is not valid, even though the file has been successfully saved as .csv, therefore I am not able to continue with the process of importing the data into my database.</p>
  
  <p>Below are the screenshots of my error:
  <img src="https://i.stack.imgur.com/SFtRp.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/IKh9j.png" alt="enter image description here"></p>
  
  <p>In conclusion I am having the error that the file path which the csv file is saved is not valid, although the csv file is successfully saved. Need some help from experienced. Thank You</p>