Thursday, May 13, 2010

Problem in uploading excel file

This is my method where I am sending my excel file add...
but in case of con open it is throwing exception "The file is allready open or permission problem"
it is only in the case when I am deplyoing my application in server . in local its working fine.


public DataTable GetTableFromExcel(string FileName)
{
try
{
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";User Id=;Password=;Extended Properties=Excel 12.0;HDR=NO;IMEX=1";
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Mode=ReadWrite;ReadOnly=false;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";
str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Mode=ReadWrite;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
using (connection = factory.CreateConnection())
{
connection.ConnectionString = str;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "Select * from [Sheet1$]";
connection.Open();
excelda = new OleDbDataAdapter();
excelda.SelectCommand = command;
dt = new DataTable();
excelda.Fill(dt);
}
}

}
catch (Exception)
{
}
finally
{
connection.Close();
}
return dt;
}

4 comments:

  1. I have done all possible things whatever I got with the help of google but nothing is working.

    ReplyDelete
  2. Hi Jay,

    I think there is a permission problem with your excel file. so first give permission to ASPNET user for your file at server. and then try again.

    thanks

    ReplyDelete
  3. Its okay if i give permission to that file but this is devloper point of view but what will happen in case of end user.. This is the main question

    ReplyDelete
  4. Thanks.. I have allready given all the permission at server but still there is a porblem.

    ReplyDelete