none
c# runs fine in release but not in batch file RRS feed

  • Question

  • Batch File #1 ========================================================== rem @echo off cd EDI_Part_Update_DB_Console2 LabelFiles2 "\\clncustools02\c$\Global Label Formats\Label Data Sets\Ford LAP Mix-Master Label Dataset.xls" "C:\EDI_Part_Update_DB_Console2" "\\Bemivas01\Public\IT\EDIFiles\862Test\*.xlsx" "C:\EDI_Part_Update_DB_Console2\ExcelCollection" ========================================================== Batch File #2 ========================================================== rem @echo off rem Excel Database File copy %1 %2 rem Excel Files used to modify Excel Database File copy %3 %4 rem Start "Ford_EDI_Parts_Update_2.exe" %1 %2 %4 Ford_EDI_Parts_Update_2.exe %1 %2 %4 rem pause ========================================================== Program.cs ========================================================== class Program { static void Main(string[] args) { string strFileDatabaseNamePath = "", strFileDatabaseName = "", strPathDBTo = "", strPath = "", strPathSpreadSheetsTo = "", strFileDatabaseNameTemp = "", strFileNameOnly = ""; string[] arrFiles = null; Process[] process1 = null; int intResult = 0, intPos = 0; ErrorLog.Log("Welcome to frmMain b4 InitializeComponent()", "Exe is Running", "C:\\LogFile\\" + Assembly.GetCallingAssembly().GetName().Name + "\\"); Thread.Sleep(100); ErrorLog.Log("Welcome to frmMain af InitializeComponent()", "Exe is Running", "C:\\LogFile\\" + Assembly.GetCallingAssembly().GetName().Name + "\\"); Thread.Sleep(100); strFileDatabaseNamePath = args[0].Replace("\r\n", ""); strPathDBTo = args[1].Replace("\r\n", ""); intPos = strFileDatabaseNamePath.LastIndexOf("\\"); strFileDatabaseName = strFileDatabaseNamePath.Substring(intPos + 1, strFileDatabaseNamePath.Length - intPos - 1); strPathSpreadSheetsTo = args[2].Replace("\r\n", ""); strPath = strPathSpreadSheetsTo.Replace("*.xlsx", ""); intResult = FileToProcess.ReadRowsInSSheet(ref strFileDatabaseName, strPathDBTo); if (intResult == 1) { ========================================================== FilesToProcess.cs ========================================================== public class FileToProcess { //Bookmark -- Use a network path public static int ReadRowsInSSheet(ref string strFileName, string strAppPath) { string strShipToPart = "", strSuppPart = "", strDesc = "", strHomeLoc = "", strCarLine = "", strPartFam = "", strCarDesig = "", strModelYr = "", strSuppName = "", strCountryOfOrigin = "", strShipTo1 = "", strSuppCode = "", strProgram = "", strRetPartNum = "", strStdPack = "", strTareWgt = "", strUnits = "", strContHgt = "", strUnitsINorMM = "", strWgtPerPiece = "", strUnits2 = "", strStdPackPerCont = "", strPartsPerPallet = "", strDock = "", strLF = "", strReserveLF = "", strFordSuppCode = "", strShipTo2 = "", strShipTo2Name = ""; int intRow = 2, intRet = 0; Microsoft.Office.Interop.Excel.Application oXLFrom = null; Database2 db = new Database2(); _Workbook oWBFrom = null; oXLFrom = new Microsoft.Office.Interop.Excel.Application(); oXLFrom.Interactive = false; Thread.Sleep(100); try { oWBFrom = oXLFrom.Workbooks.Open(strAppPath + "\\" + strFileName); } catch (Exception ex) { ErrorLog.Log(ex, "Open Excel File", "C:\\LogFile\\" + Assembly.GetCallingAssembly().GetName().Name + "\\", "FileToProcess.ReadRowsInSSheet.Open Method:"); CloseExcelProcesses_dll.Program.CloseProjectExcelFiles(ref oXLFrom); return -1; } Thread.Sleep(100); ErrorLog.Log("B4 Truncate", "Exe is Running", "C:\\LogFile\\" + Assembly.GetCallingAssembly().GetName().Name + "\\"); Thread.Sleep(100); intRet = db.Truncate(); Thread.Sleep(100); ========================================================== Database2.cs ========================================================== public class Database2 { int x = 5, y = 0; public int Truncate() { MySqlConnection conn; MySqlCommand cmd; ErrorLog.Log("Welcome To Truncate", "Exe is Running", "C:\\LogFile\\" + Assembly.GetCallingAssembly().GetName().Name + "\\"); Thread.Sleep(100); conn = null;

    //Blows up here -- Cannot Open Connection using (conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["FordEDIParts"].ConnectionString)) { ==========================================================

    Why would it open the connection and have no problems in release mode but not able to emulate when exe is run from a batch file?

    Thursday, July 18, 2019 5:07 PM

Answers

  • thanks Chris, but the parameters were not the problem. Plus, the two copy statements were working perfectly.

    The problem was that it was not able to connect to my database while the program was run through a batch file.  Only when the project was run through Visual Studio (debug or release mode).

    My Project is in one folder and my published app for the project is in another folder.  I kept on copying my new exe to the app folder but I did not copy over the exe.config.  This contains the App.Config logic for opening the database as apparently, when you do a rebuild, it does not embed the App.Config logic  into the newly created exe.  It keeps it in the external exe.config file.

    • Marked as answer by OldeEnglishD Friday, July 19, 2019 3:13 PM
    Friday, July 19, 2019 3:13 PM

All replies

  • When I meant run in release mode, I meant release mode when running in visual studio.  That is working fine!
    Thursday, July 18, 2019 8:15 PM
  • Hi,

    try this:

    copy "%1" "%2"
    rem Excel Files used to modify Excel Database File
    copy "%3" "%4"
    rem Start "Ford_EDI_Parts_Update_2.exe" "%1" "%2" "%4"
    Ford_EDI_Parts_Update_2.exe "%1" "%2" "%4"

    Greetings,

    Chris

    Thursday, July 18, 2019 8:28 PM
  • thanks Chris, but the parameters were not the problem. Plus, the two copy statements were working perfectly.

    The problem was that it was not able to connect to my database while the program was run through a batch file.  Only when the project was run through Visual Studio (debug or release mode).

    My Project is in one folder and my published app for the project is in another folder.  I kept on copying my new exe to the app folder but I did not copy over the exe.config.  This contains the App.Config logic for opening the database as apparently, when you do a rebuild, it does not embed the App.Config logic  into the newly created exe.  It keeps it in the external exe.config file.

    • Marked as answer by OldeEnglishD Friday, July 19, 2019 3:13 PM
    Friday, July 19, 2019 3:13 PM