I have created a C# windows application that takes 1 command line argument which is country code and generates an output to excel file
Now there are 20 country(Arguments) that i want to process on a specific schedule but there will not be always data available for all 20 countries
So instead of creating 50 schedules calling main interface with 20 country code, I created one more Application that will check if data is available for let's say 5 countries only then invoke the main application 5 times with country code
now my problem is when i call the main application trough command prompt it is working fine and generating the output to excel
but when i only executes the Invoker application, the main applications starts do all the work but does not generate the excel
What I have tried:
This works perfectly fine
D:\UAT>ItemMaster_UAT.exe KSA
Invoker Code
strq = @"select DISTINCT COUNTRY FROM SHIPMENT_DETAIL WHERE ISPROCESSED=0";
dt = bc.GetDatatable(strq);
foreach (DataRow dr in dt.Rows)
{
ProcessStartInfo startInfo = new ProcessStartInfo(ExePath, dr["Country"].ToString());
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = false;
Process p = new Process() { StartInfo = startInfo };
bool isStarted = p.Start();
Error that i am receiving
Error while executing funtion DataSetsToExcel()
Error Message --> Exception from HRESULT: 0x800A03EC
Stack Trace --> at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
at PO_FileGenerator.AFR_Item_Master.DataSetsToExcel(List`1 dataSets, String fileName, String FilePath)
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = null;
foreach (DataSet dataSet in dataSets)
{
System.Data.DataTable dataTable = dataSet.Tables[0];
int rowNo = dataTable.Rows.Count;
int columnNo = dataTable.Columns.Count;
int colIndex = 0;
xlSheets = xlWorkbook.Sheets;
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlWorksheet.Name = dataSet.Tables[0].TableName;
foreach (DataColumn dataColumn in dataTable.Columns)
{
colIndex++;
xlApp.Cells[1, colIndex] = dataColumn.ColumnName;
}
object[,] objData = new object[rowNo, columnNo];
for (int row = 0; row < rowNo; row++)
{
for (int col = 0; col < columnNo; col++)
{
objData[row, col] = dataTable.Rows[row][col].ToString();
}
}
Range range = xlWorksheet.Range[xlApp.Cells[2, 1], xlApp.Cells[rowNo + 1, columnNo]];
range.Value2 = objData;
colIndex = 0;
foreach (DataColumn dataColumn in dataTable.Columns)
{
colIndex++;
string format = "@";
switch (dataColumn.DataType.Name)
{
case "Boolean":
break;
case "Byte":
break;
case "Char":
break;
case "DateTime":
format = "dd-mmm-yy";
break;
case "Decimal":
format = "$* #,##0.00;[Red]-$* #,##0.00";
break;
case "Double":
break;
case "Int16":
format = "0";
break;
case "Int32":
format = "0";
break;
case "Int64":
format = "0";
break;
case "SByte":
break;
case "Single":
break;
case "TimeSpan":
break;
case "UInt16":
break;
case "UInt32":
break;
case "UInt64":
break;
default:
break;
}
xlWorksheet.Range[xlApp.Cells[2, colIndex], xlApp.Cells[rowNo + 1, colIndex]].NumberFormat = format;
}
}
((Microsoft.Office.Interop.Excel.Worksheet)xlApp.ActiveWorkbook.Sheets[xlApp.ActiveWorkbook.Sheets.Count]).Delete();
xlWorkbook.SaveAs(FilePath + fileName);
xlWorkbook.Close();
xlApp.Quit();
GC.Collect();
}
catch (Exception ex)
{
bc.sbLog.AppendLine("Error while executing funtion DataSetsToExcel()");
bc.sbLog.AppendLine("Error Message --> " + ex.Message.ToString());
bc.sbLog.AppendLine("Stack Trace --> " + ex.StackTrace.ToString());
}