Search code examples
c#asp.netwindows-server-2012filenotfoundexceptionsqlcmd

SQLCMD was not found when running Process from ASP .NET


I have an ASP .NET application that starts sqlcmd.exe as a Process. In two of our three test environments, we have no problems. However, on the third machine, even though sqlcmd.exe was installed along with client connectivity tools, Process.exe cannot find sqlcmd.exe. The error shown is:

Error running process: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
       at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
       at L3_CNM.Utilities.Common.SqlcmdHelper.RunRemoteScript(String ServerAddress, String datbaseName, String filePath, String outputFilePath

I am at a loss for why this happens. The differences between the case when everything is fine as opposed to when it fails are:

1) When it works, there is a full SQL server installation. When it fails, we are only installing sqlcmd as a downloaded installation package which points to a SQL server that resides elsewhere.

2) When it works, the application is running from the same disk volume as Windows installation. On the machine that fails, the application is installed on another volume from the Windows installation.

Other key points - the PATH variable shows the location to sqlcmd.exe, the user that is the application pool identity is a part of the local system administrators, when running sqlcmd.exe through command prompt the results are expected.

Any help would be highly appreciated.

Thanks

EDIT: Adding code:

try
{
    Process process = new Process();
    process.StartInfo.FileName = "sqlcmd.exe";
    Logging.Instance.Log(Logging.Levels.Message, "Process start arguments: " + process.StartInfo.Arguments);
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.CreateNoWindow = true;
    if (process.Start())
        Logging.Instance.Log(Logging.Levels.Message, "Process successfully started");
    else
    {
        Logging.Instance.Log(Logging.Levels.Message, "Unable to start process");
    }
    string output = process.StandardOutput.ReadToEnd();
    output += process.StandardError.ReadToEnd();
    process.WaitForExit();
    Logging.Instance.Log(Logging.Levels.Message, "Process exited with output: " + output);
    return output;
}
catch (Exception e)
{
    Logging.Instance.Log(Logging.Levels.Error, "Error running process: " + e.ToString());
    return e.ToString();
}

Solution

  • A simple Reboot. God I hate Windows sometimes.

    Thanks @Methodman for the suggestion.