0

Well, I'm doing a program that imports data from an excel file into a database(sql server). The program runs fine if I save the excel file in IIS express folder but if I put in documents folder or something like it, it gives me an error:

here's the code:

protected void Upload_Click(object sender, EventArgs e)
    {
        string filepath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filepath);
        string ext = Path.GetExtension(filename);
        String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";

        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

        OleDbCommand cmd = new OleDbCommand("Select [Name],[City],[Age] from [Sheet1$]", excelConnection);
        excelConnection.Open();

        //cmd.ExecuteNonQuery();


       // DataSet ds = new DataSet();
       // SqlDataAdapter da = new SqlDataAdapter("Select [Name],[City],[Age] from [sheet1$]", strConnection);

        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

        sqlBulk.DestinationTableName = "Test";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();
    }
  • What error....? – Patrick Hofman May 03 '19 at 10:11
  • @Patrick Hofman :IErrorInfo.GetDescription com E_FAIL(0x80004005). –  May 03 '19 at 10:16
  • The process the IIS/Cassini runs under does not have permission to that folder. Consider writting the file to a folder with enough rights, doing the bulk copy, and deleting afterwards. – Cleptus May 03 '19 at 10:17
  • @bradbury9 Sorry for being ignorant, but I'm not understanding how im gonna do that... –  May 03 '19 at 10:21
  • Just check [Can a Byte Array be written to a file in C#?](https://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file-in-c) and [How do I change folder and file Permissions?](https://answers.microsoft.com/en-us/windows/forum/windows_vista-files/how-do-i-change-folder-and-file-permissions/465f2b42-63dd-4486-8dd1-c870290efeed) – Cleptus May 03 '19 at 11:00
  • @bradbury9 I want to save in a folder like "files" to be able to upload in whatever the excel file is, into database (sql server) But the problem keeps showing me the error when I do not upload the file in IIS express folder –  May 03 '19 at 11:30

2 Answers2

0

Try

   string filepath = Server.MapPath(FileUpload1.FileName);
Joey Phillips
  • 1,543
  • 2
  • 14
  • 22
  • it gave me this error The Microsoft Access database engine could not find the object 'C:\Users\Pedro\source\repos\WebApplication5\WebApplication5\123.xls'. Make sure the object exists and that you spell its name and the path name correctly. If 'C:\Users\Pedro\source\repos\WebApplication5 –  May 06 '19 at 08:10
  • Glad I could help! If my answer was helpful consider making it the accepted answer : ) – Joey Phillips May 06 '19 at 12:15
0
 string filepath = Server.MapPath("~/Files/")Path.GetFileName(FileUpload1.FileName);