I am trying to upload an excel file into SQL server. The thing is I am stuck here
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
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
<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
https://www.c-sharpcorner.com/blogs/how-to-import-excel-data-in-sql-server-using-asp-net2
and
https://www.aspdotnet-suresh.com/2012/12/aspnet-how-to-get-full-file-path-from.html
and changed my code to
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("Files/" + filename));
string filepath = "Files/" + filename;
I got Could not find a part of the path exception.
$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
I then referred to the following also -
How to get the path of a file which is in file upload control in asp.net
but same error
I then inspected the path in the inspection and found out that it is incorrect --
C:\\Users\\AMEN\\source\\repos\\ExcelDatabase\\ExcelDatabase\\Files\\New Microsoft Excel Worksheet.xlsx'
I dont know why it entered the repos folder when I selected my excel from the desktop. But I know it appended file directory because of the following line
FileUpload1.SaveAs(Server.MapPath("Files/" + filename));
So I commented it out but then again too, it took the wrong path and I got the same exception . Please suggest me what should I do. Thanks