Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:13:38+00:00 2026-05-26T17:13:38+00:00

I am trying to import a .csv file into my database. I am able

  • 0

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.

Below is my 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" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
    {
        Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
        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=<IP>;Initial Catalog=<DB>;User ID=<userid>;Password=<password>";

        // Bulk Copy to SQL Server
        using (SqlBulkCopy bulkCopy =
                   new SqlBulkCopy(sqlConnectionString))
        {
            bulkCopy.DestinationTableName = "DEMUserRoles";
            bulkCopy.WriteToServer(dr);
            Response.Write("<script>alert('DEM User Data imported');</script>");

        }
    }
    }
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");

}

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.

Below are the screenshots of my error:
enter image description here
enter image description here

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:13:39+00:00Added an answer on May 26, 2026 at 5:13 pm

    The connection string Data Source should only contain the path to your CSV file. It should not contain the CSV file name.

    The file name is specified in the SQL statement as a table.

    string dir = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\";
    string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
    
    string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
    
    OleDbCommand ExcelCommand = new OleDbCommand(
        "SELECT [columns] FROM " + mycsv, ExcelConnection);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to import a txt/csv file into my postgres database from php
I am trying to import a .csv file into my database. I am able
I'm trying to load data from a CSV file into a MySQL database, and
I am trying to import a CSV file into a MySQL database table. The
I am trying to import a large CSV file into a MySQL database. I
I am trying to import a CSV file into a MySQL 5.1 database. The
I am trying to import a csv file to insert data into an existing
I'm trying to import a matrix (about 80.000 rows) from a csv file to
I am trying to import an excel file into a data table using GemBox
I am trying to build a import form for a CSV file into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.