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

  • Home
  • SEARCH
  • 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 7879241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:51:33+00:00 2026-06-03T03:51:33+00:00

I am developing this simple application to upload an Excel file ( .xlsx )

  • 0

I am developing this simple application to upload an Excel file (.xlsx) and import the data present in that Excel worksheet into a SQL Server Express database in .NET

I’m using the following code on click of the import button after browsing and selecting the file to do it.

protected void Button1_Click(object sender, EventArgs e)
{
        String strConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Users\\Hemant\\documents\\visual studio 2010\\Projects\\CRMdata\\CRMdata\\App_Data\\Database1.mdf';Integrated Security=True;User Instance=True";
        //file upload path
        string path = FileUpload1.PostedFile.FileName;
        //string path="C:\\ Users\\ Hemant\\Documents\\example.xlsx";
        //Create connection string to Excel work book
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        //Create Connection to Excel work book
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection);
        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
        //Give your Destination table name
        sqlBulk.DestinationTableName = "Excel_table";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();
    }

But the code doesn’t run when I use

string path = FileUpload1.PostedFile.FileName;`

and even

string path="C:\ Users\ Hemant\Documents\example.xlsx";` 

The dReader is unable to take the path in this format.

It is only able to take path in the following format

string path="C:\\ Users\\ Hemant\\Documents\\example.xlsx";

i.e. with the the \\ in the path.For which I have to hard code the path but we have to browse the file.

So,can any one please suggest a solution to use the path taken by the FileUpload1 to import the data?

  • 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-06-03T03:51:33+00:00Added an answer on June 3, 2026 at 3:51 am

    You are dealing with a HttpPostedFile; this is the file that is “uploaded” to the web server. You really need to save that file somewhere and then use it, because…

    …in your instance, it just so happens to be that you are hosting your website on the same machine the file resides, so the path is accessible. As soon as you deploy your site to a different machine, your code isn’t going to work.

    Break this down into two steps:

    1) Save the file somewhere – it’s very common to see this:

    string saveFolder = @"C:\temp\uploads"; //Pick a folder on your machine to store the uploaded files
    
    string filePath = Path.Combine(saveFolder, FileUpload1.FileName); 
    
    FileUpload1.SaveAs(filePath);
    

    Now you have your file locally and the real work can be done.

    2) Get the data from the file. Your code should work as is but you can simply write your connection string this way:

    string excelConnString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties="Excel 12.0";", filePath);
    

    You can then think about deleting the file you’ve just uploaded and imported.

    To provide a more concrete example, we can refactor your code into two methods:

        private void SaveFileToDatabase(string filePath)
        {
            String strConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Users\\Hemant\\documents\\visual studio 2010\\Projects\\CRMdata\\CRMdata\\App_Data\\Database1.mdf';Integrated Security=True;User Instance=True";
    
            String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
            //Create Connection to Excel work book 
            using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
            {
                //Create OleDbCommand to fetch data from Excel 
                using (OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection))
                {
                    excelConnection.Open();
                    using (OleDbDataReader dReader = cmd.ExecuteReader())
                    {
                        using(SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
                        {
                            //Give your Destination table name 
                            sqlBulk.DestinationTableName = "Excel_table";
                            sqlBulk.WriteToServer(dReader);
                        }
                    }
                }
            } 
        }
    
    
        private string GetLocalFilePath(string saveDirectory, FileUpload fileUploadControl)
        {
    
    
            string filePath = Path.Combine(saveDirectory, fileUploadControl.FileName);
    
            fileUploadControl.SaveAs(filePath);
    
            return filePath;
    
        }
    

    You could simply then call SaveFileToDatabase(GetLocalFilePath(@"C:\temp\uploads", FileUpload1));

    Consider reviewing the other Extended Properties for your Excel connection string. They come in useful!

    Other improvements you might want to make include putting your Sql Database connection string into config, and adding proper exception handling. Please consider this example for demonstration only!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im developing a simple application that have a line like this: string[] values =
I am developing a simple application which hava a file Constants.py containing all configuration,
I'm developing a simple application that animates an image as the user moves a
I was developing a simple SMS application using the Email-SMS Gateways that various Phone
This is probably a simple question but I am developing a web app in
I am developing a simple sip-based voip app for android. I used this sample
I'm developing this application where you can put text and drawings in a page.
I am developing this JavaScript game, that manages it's loading of assets, prior anything
So we are a few guys developing this product that is communicating with a
I am developing a small, internal-use only web application. Given its simple nature and

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.