I'm having trouble getting this error resolved. My purpose is to collect data from an Excel file storing it in C # variables, which in turn will be inserted in SQL Server 2012. Reading is done line by line. Here is the code where the error happens:
Excel.Application excelApp = new Excel.Application();
Excel.Workbook WB = null;
Excel.Worksheet WS = null;
excelApp.Visible = false;
Convert.ToString(FileUpload1.PostedFile.FileName);
Response.Write(FileUpload1.PostedFile.FileName);
string excelpath = FileUpload1.PostedFile.FileName;
Convert.ToString(excelpath);
string workbookPath = excelpath;
WB = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
WS = excelApp.Worksheets.get_Item("" + TextBox2.Text);
String connectionString =
"Data Source=localhost;" +
"Initial Catalog=Teste;" +
"User id=sa;" +
"Password=123;";
Excel.Range last = WS.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell);
Excel.Range range = WS.get_Range("A1", last);
object ranger = range.Value;
Error:
An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code asp.net