Thursday, June 4, 2009

Unspecfied Error OledbException

System.Data.OleDb.OleDbException: Unspecified error at System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) at System.Data.OleDb.OleDbConnection.InitializeProvider() at System.Data.OleDb.OleDbConnection.Open() at

Cause:
You use Microsoft.JET OleDB Provider to open connection to Excel file or Access using windows forms or console application in .net/C# and you get this error.You most probably use a windows service account for running the application that uses above code.

Solution:
The Microsoft JET writes temporary files to the folder identified by TEMP user environment variable.For service accounts, due to server policies, this folder may be removed periodically.
The solution is to progammatically create this folder first if it doesn't exist so that this dreaded error doesn't come.

System.Environment.GetEnvironmentVariable("temp") will get you the variable identified by temp. Create this folder using this code piece.

if(!System.IO.Directory.Exists(System.Environment.GetEnvironmentVariable("temp")))
{
System.IO.Directory.CreateDirectory(System.Environment.GetEnvironmentVariable("temp"));
}

Hope this helps.

No comments:

Post a Comment