I am not sure why I am getting a System.OutOfMemoryException
in SharpDevelop for my program in C#. My program opens up an Excel worksheet and processes some of the data in the worksheet to check for duplicates.
Here is the complete Exception error message:
System.Runtime.InteropServices.COMException: See inner exception(s) for details. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OutOfMemoryException: Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))
at static Object NetOffice.Invoker.PropertyGet(NetOffice.COMObject comObject, System.String name, System.Object[] paramsArray)
at Object NetOffice.ExcelApi.Range.get_Value()
at System.Void excelApp.Program.markDuplicates() in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 80
at static System.Void excelApp.Program.Main(System.String[] args) in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 41
Here is my complete program:
It is pointing at the following line in the markDuplicates()
method:
Line 80:
Object[,] valuesArray = (Object[,])tableRange.Value;
I have no idea why I am getting this exception.
I am using .NET Framework 4.5.1.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input.Manipulations;
using NetOffice.ExcelApi;
using NetOffice.ExcelApi.Enums;
using Excel = NetOffice.ExcelApi.Application;
namespace excelApp
{
class Program
{
Excel excelApplication;
Workbook workbook;
Worksheet sheet;
HashSet<int> mpanHashCodeList;
[STAThreadAttribute]
public static void Main(string[] args)
{
Program p = new Program();
p.openWorkSheet(@"C:\Users\HP\Desktop\Book1.xlsx", 2);
p.markDuplicates();
Console.ReadKey(true);
}
private void openWorkSheet(string path, int worksheet)
{
excelApplication = new Excel
{
Visible = true,
ScreenUpdating = true
};
try
{
workbook = excelApplication.Workbooks.Open(path);
sheet = (Worksheet)workbook.Worksheets[worksheet];
}
catch
{
Console.WriteLine("File does not exist");
}
}
private void markDuplicates()
{
Range range = sheet.Cells[2,2];
Range rngLastCell = range.get_End(XlDirection.xlToRight)
.get_End(XlDirection.xlDown);
// holds the range of cells in the worksheet
Range tableRange = sheet.Range(range, rngLastCell);
// holds all the values of the range of cells in the worksheet
Object[,] valuesArray = (Object[,])tableRange.Value;
mpanHashCodeList = new HashSet<int>();
int count = 0;
for(var i = 1; i <= valuesArray.GetUpperBound(0); i++)
{
// create a new string for each row
var rowIdBuilder = new StringBuilder(10);
for(var j = 1; j <= valuesArray.GetUpperBound(1); j++)
{
switch(j)
{
case 1:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 3:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 4:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 6:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
}
}
Console.WriteLine(rowIdBuilder.ToString());
int hashcode = rowIdBuilder.ToString().GetHashCode();
if(mpanHashCodeList.Contains(hashcode))
{
count++;
mpanHashCodeList.Remove(hashcode);
}
else
{
mpanHashCodeList.Add(hashcode);
}
}
Console.WriteLine(count + " duplicates found");
}
}
}
It's quite common for native code to return error code 0x8007000E (E_OUTOFMEMORY), which is then mapped to OutOfMemory exception in managed code. For example, this is common returned when a native heap allocations fails, or a handle allocation fails.
Such problem is best diagnosed with mixed mode debugging in Visual Studio. Pros would normally use tools like WinDbg/cdb for such tasks with the right symbols properly resolved.
In short: no simple answer. Dirty work expected.
User contributions licensed under CC BY-SA 3.0