sharmila
sharmila

Reputation: 1533

error while reading excel file

I am using the below code to get data from excel file. While opening the connection I get the below error. can any one tell me how can I solve this issue?

Request for the permission of type 'System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Note: ExcelFilePath is the path of excel file stored in local drive.

String connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", ExcelFilePath);

 DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

            DbConnection connection = factory.CreateConnection();

            connection.ConnectionString = connectionString;

            connection.Open(); // GIVES ERROR 

            DataTable tbl = connection.GetSchema("Tables");

            connection.Close();

            foreach (DataRow row in tbl.Rows)
            {
                //get sheets.

            }

Upvotes: 1

Views: 1057

Answers (2)

Dany
Dany

Reputation: 2174

If you are using 32 bit system then config file is at location

%windir%\Microsoft.NET\Framework\{version}\CONFIG

If you are using 64 bit system then config file is at location

%windir%\Microsoft.NET\Framework64\{version}\CONFIG

Try this.

The exception you encountered indicates that the C# assembly containing the stored procedure that you registered has insufficient permissions to create an OleDbConnection object, with respect to the .Net framework Code Access Security (CAS) policies. This probably means that your assembly was registered WITH PERMISSION_SET = SAFE, which is also the default if PERMISSION_SET is unspecified in the CREATE ASSEMBLY DDL statement.

To fix this, you can re-register your assembly WITH PERMISSION_SET = EXTERNAL_ACCESS to gain access to the ADO.Net classes (including OleDbConnection). If you are using Visual Studio, you will have to make this change from SAFE to EXTERNAL_ACCESS in the properties page of your C# database project.

For More Details Check http://msdn.microsoft.com/en-us/library/0x4t63kb.aspx

Upvotes: 0

Tariqulazam
Tariqulazam

Reputation: 4585

This error is normally caused by code access security. Try to run your application in full trust mode.

<trust level="Full" originUrl="" />

Upvotes: 2

Related Questions