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 7919447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:52:06+00:00 2026-06-03T15:52:06+00:00

I want to copy an excel file to my database. The problem I’m having

  • 0

I want to copy an excel file to my database. The problem I’m having is I cant seem to copy the path from into the code-behind. This is my asp code:

    <asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />

if I hard code in a path then my code will work:

string path = @"C:\Users\moynik\Desktop\datatest.xls";

but if I use the following line I only get a different path that will not work.

string path = Path.GetFullPath(FileUploadControl.FileName);

but I need to make my path dynamic so users can upload from different destinations on there own computer. Can anyone help me out. this is the function which works and copys the excel file to my database

If this is impossible can someone help me with a solution if they think of one. Thanks

protected void UploadButton_Click(object sender, EventArgs e)
{
    if (FileUploadControl.HasFile)
    {
        try
        {
            string path = @"C:\Users\moynik\Desktop\datatest.xls";
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";



    using (OleDbConnection connection = new OleDbConnection(connStr))
            {
                OleDbCommand command = new OleDbCommand ("Select Name,Address,Age FROM [Sheet1$]", connection);

                connection.Open();

                using (DbDataReader dr = command.ExecuteReader())
                { 
                    string sqlConnectionString = "SERVER=<servername>;UID=schafc;Trusted_Connection=Yes;DATABASE=<dbname>;";

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                    {
                        bulkCopy.DestinationTableName = "<tableName>";
                        bulkCopy.WriteToServer(dr);
                    }
                }
            } 
            StatusLabel.Text = "Upload status: File uploaded!";
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}
  • 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-03T15:52:09+00:00Added an answer on June 3, 2026 at 3:52 pm

    Try Path.GetFileName(FileUploadControl.FileName) instead of .GetFullPath.

    http://asp.net-tutorials.com/controls/file-upload-control/

    EDIT: Ah, I’ve just worked out what you’re trying to do. You need to upload the xls file to a server location first, and then you can pass the path to the file on the server into the connection string.

    e.g.

    string filename = Path.GetFileName(FileUploadControl.FileName);
    string path = Server.MapPath("~/") + filename;
    FileUploadControl.SaveAs(path);
    
    string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to copy tabular data to Excel from my app. The most simple
I want to copy particular columns from one Excel sheet to another Excel sheet
I have an Excel-Macro in VBA in which I want to copy the file
Our users want to copy and paste some rows from an Excel spreadsheet, or
I want to support copy/paste functionality of Excel data from the windows clipboard onto
I want to copy contents of one row in Excel to other row. Currently,
I want to copy the contents of a DataGridView and paste it in Excel.
I copy search results of Google and want to stick it on Excel now.
I want to copy a binary master file in a new binary file. This
I want to copy some functions from OpenCV library to my embedded application. Rewriting

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.