I'm using the following code to update a cell in an Excel file.
public bool WriteChange(string Filename, string SheetName, string Cell, string Value)
{
if(!System.IO.File.Exists(Filename))
{
throw new System.IO.FileNotFoundException("File \"" + Filename + "\" could not be found");
}
bool result = false;
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO;\"";
string SQL = "UPDATE [" + SheetName + Cell + ":" + Cell + "] SET F1='" + Value + "'";
using(OleDbConnection Connection = new OleDbConnection(ConnectionString))
{
Connection.Open();
using(OleDbCommand cmd = new OleDbCommand(SQL,Connection))
{
int value = cmd.ExecuteNonQuery();
if(value > 0)
{
result = true;
}
}
}
return result;
}
Which works fine, unless I try to update the same cell multiple times. Once a cell has been updated using this function, it can never be updated using this function again. If I try to update the cell again; even after restarting the application, I get an OleDbException: System Resource Exceeded
.
I know you typically receive this exception if you are creating a bunch of connections to the spreadsheet (in a loop for example), but I am only connecting once per application run. The typical work flow is.
- Start application.
- Call
WriteChange
. - Exit application.
Why am I getting this error, when the connection should be long dead by the time I try to connect again?