error reading excel using OleDb

G

Guest

Hi
I try to execute an sample how to read and write excel sheets using OleDb
When opens the connection, the objConn.Open gave me this error:
"Request for the permission of type 'System.Data.OleDb.OleDbPermission,
System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed."
And appears the following message:
System.Exception {System.Security.SecurityException}
Any idea to solve this error?
I work with VS 2005, using C#
TIA
Abel
 
J

Jim Hughes

Show the code.

State the environment (Winforms, ASP.Net) OS, Office version etc.
 
G

Guest

Hi
This is an winforms program, but fails in a second class program, like
backoffice, in a ASP 2.0 program

This is my code:


public System.Data.DataSet GetDataFromExcelFile(String File_name) {
try {
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;";
strConn += "Data Source= " + File_name + "; Extended Properties=Excel
8.0;HDR=Yes;IMEX=1";
OleDbConnection ObjConn = new OleDbConnection(strConn);
====> here fails => ObjConn.Open();
OleDbCommand ObjCmd = new OleDbCommand("SELECT * FROM [ExcelFileTest$]",
ObjConn);
OleDbDataAdapter objDA = new OleDbDataAdapter();
objDA.SelectCommand = ObjCmd;
DataSet ObjDataSet = new DataSet();
objDA.Fill(ObjDataSet);
ObjConn.Close();
return ObjDataSet;
} catch(Exception excepcion) {
return null;
}
}

This is an environment problem
You can see on the stacktrace:

StackTrace " at System.Security.CodeAccessSecurityEngine.Check(Object
demand, StackCrawlMark& stackMark, Boolean isPermSet)\r\n at
System.Security.PermissionSet.Demand()\r\n at
System.Data.Common.DbConnectionOptions.DemandPermission()\r\n at
System.Data.OleDb.OleDbConnection.PermissionDemand()\r\n at
System.Data.OleDb.OleDbConnectionFactory.PermissionDemand(DbConnection
outerConnection)\r\n at
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.OleDb.OleDbConnection.Open()\r\n at
ProcessExcelFile.Form1.GetDataFromExcelFile(String File_name)"

Understand?

Regards

Abel
 
P

Paul Clement

¤ Hi
¤ I try to execute an sample how to read and write excel sheets using OleDb
¤ When opens the connection, the objConn.Open gave me this error:
¤ "Request for the permission of type 'System.Data.OleDb.OleDbPermission,
¤ System.Data, Version=2.0.0.0, Culture=neutral,
¤ PublicKeyToken=b77a5c561934e089' failed."
¤ And appears the following message:
¤ System.Exception {System.Security.SecurityException}
¤ Any idea to solve this error?
¤ I work with VS 2005, using C#

Where is the Excel file located? Local? Network?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
J

Jim Hughes

By default, IIS using ASP.Net will not have access to mapped drives or UNC
paths on remote servers.

Winforms apps will have whatever access the user running them has.

Abel said:
Hi
This is an winforms program, but fails in a second class program, like
backoffice, in a ASP 2.0 program

This is my code:


public System.Data.DataSet GetDataFromExcelFile(String File_name) {
try {
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;";
strConn += "Data Source= " + File_name + "; Extended Properties=Excel
8.0;HDR=Yes;IMEX=1";
OleDbConnection ObjConn = new OleDbConnection(strConn);
====> here fails => ObjConn.Open();
OleDbCommand ObjCmd = new OleDbCommand("SELECT * FROM [ExcelFileTest$]",
ObjConn);
OleDbDataAdapter objDA = new OleDbDataAdapter();
objDA.SelectCommand = ObjCmd;
DataSet ObjDataSet = new DataSet();
objDA.Fill(ObjDataSet);
ObjConn.Close();
return ObjDataSet;
} catch(Exception excepcion) {
return null;
}
}

This is an environment problem
You can see on the stacktrace:

StackTrace " at System.Security.CodeAccessSecurityEngine.Check(Object
demand, StackCrawlMark& stackMark, Boolean isPermSet)\r\n at
System.Security.PermissionSet.Demand()\r\n at
System.Data.Common.DbConnectionOptions.DemandPermission()\r\n at
System.Data.OleDb.OleDbConnection.PermissionDemand()\r\n at
System.Data.OleDb.OleDbConnectionFactory.PermissionDemand(DbConnection
outerConnection)\r\n at
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.OleDb.OleDbConnection.Open()\r\n at
ProcessExcelFile.Form1.GetDataFromExcelFile(String File_name)"

Understand?

Regards

Abel



Jim Hughes said:
Show the code.

State the environment (Winforms, ASP.Net) OS, Office version etc.
 
G

Guest

Paul
This is a winform program
The file is located in the same computer, on the local c: disk
Is not reason for this error
TIA
Abel
 
P

Paul Clement

¤ Paul
¤ This is a winform program
¤ The file is located in the same computer, on the local c: disk
¤ Is not reason for this error
¤ TIA
¤ Abel
¤

Are you running the application from the local computer as well or from the network?


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top