Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Shell
D:\UAT>ItemMaster_UAT.exe KSA


Invoker Code
C#
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)

C#
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;

                    //Create Excel Sheets
                    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;
                    //if (dataSet.Tables[0].TableName == "Item_VAT")
                    //{
                    //    MessageBox.Show("asad");
                    //}

                    //Generate Field Names
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        colIndex++;
                        xlApp.Cells[1, colIndex] = dataColumn.ColumnName;
                    }

                    object[,] objData = new object[rowNo, columnNo];

                    //Convert DataSet to Cell Data
                    for (int row = 0; row < rowNo; row++)
                    {
                        for (int col = 0; col < columnNo; col++)
                        {
                            objData[row, col] = dataTable.Rows[row][col].ToString();
                        }
                    }

                    //Add the Data
                    Range range = xlWorksheet.Range[xlApp.Cells[2, 1], xlApp.Cells[rowNo + 1, columnNo]];
                    range.Value2 = objData;

                    //Format Data Type of Columns 
                    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: //String
                                break;
                        }
                        //Format the Column accodring to Data Type
                        xlWorksheet.Range[xlApp.Cells[2, colIndex], xlApp.Cells[rowNo + 1, colIndex]].NumberFormat = format;
                    }
                }

                //Remove the Default Worksheet
                ((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());
            }
Posted
Updated 19-Aug-21 3:54am
v2

That application you are launching may be making assumptions about what the "current directory" is when working with files. The "current directory" is never what you assume it is.

Your code should NEVER assume the "current directory" is the directory the app was launched from. ALWAYS build fully qualified paths to files from well-known locations.

In your case, when you launch the application from a CMD, the "current directory" is the path shown at the CMD prompt.

When you launch it from another application, the "current directory" is probably the directory the first app was launched from and not the directory your 2nd app was launched from.
 
Share this answer
 
Comments
AwadBasmeh 22-Aug-21 5:34am    
Hi Dave
Can you share some details about "fully qualified path" or give some example.
Dave Kreskowiak 25-Aug-21 18:19pm    
"Fully qualified path" means a path to a file or folder the begins with either a drive letter, "C:\SomeFolder\filename.ext" or a UNC path, "\\servername\sharename\folderpath".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900