Question How to specify the path in File Upload Control?

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
70
Programming Experience
Beginner
Hello,

I am trying to upload an excel file into SQL server. The thing is I am stuck here

C#:
  string fileName = Path.Combine(@"C:\", FileUpload1.FileName);
                    FileUpload1.SaveAs(fileName);

                    String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", fileName);

The line
C#:
string fileName = Path.Combine(@"C:\", FileUpload1.FileName);
gives pathname as C:\\FileName for which I get an exception that access is denied, and from this Why is access to the path denied?
I got to know that we can get these errors if the path is a directory. I know my path is wrong, but I am clueless as how to select the correct path for further processing.

My asp.net code is

C#:
<asp:FileUpload ID="FileUpload1" runat="server" Width="368px" />
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </p>

When I referred this
and changed my code to
C#:
string path = string.Concat(Server.MapPath("~/UploadFile/" + FileUpload1.FileName));
                    FileUpload1.SaveAs(path);

I got Could not find a part of the path exception.
C#:
+        $exception    {"Could not find a part of the path 'C:\\Users\\AMEN\\source\\repos\\ExcelDatabase\\ExcelDatabase\\Files\\New Microsoft Excel Worksheet.xlsx'."}    System.IO.DirectoryNotFoundException
 
Last edited:
Top Bottom