I am 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 if I don't try to update the same cell multiple times. After a cell has been updated using this function, it can never be updated using this function again. If I try to refresh the cell again; even after restarting the application, I get OleDbException: System Resource Exceeded.
I know that you usually get this exception if you create a bunch of connections to a spreadsheet (for example, in a loop), but I connect only once for each application launch. Typical workflow.
- Launch the application.
- Challenge
WriteChange. - Exit the application.
, ?