none
Failing to connect to Sql Server from a C# program in Release only RRS feed

  • Question

  • Hi there,

    I need to move my C# code from Debug where it works well to Release. The program compiles and I can run the resulting exec file, however when I try to connect to Sql Server I get this exception message (runtime)

    ************** Exception Text **************
    System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.Smo, Version=14.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
    File name: 'Microsoft.SqlServer.Smo, Version=14.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
       at SQLServerSampleCodes.Form1.PushConnectToSqlServer_PG1_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

    This .dll file Microsoft.SqlServer.Smo.dll is present in both Debug and Release folders with the same version, it works for Debug but does not work for Release. The version of the file in both folders is different, however, from the version the exception is looking for, it is older.  It seems to me the system suggests a solution but I will have to mess with the Registry.

    Is it what I should do?

    Thanks, -MyCatAlex

    Tuesday, November 19, 2019 2:44 PM

Answers

  • I am confused. I will do what you suggested but that will be my last act. In principle, I can work with the debug version with the Visual Studio open for a while.

    I have never used Release mode in VS not in any professional or home usage environment. :) And I have used all the versions of VS offering the feature.

    And about VS Command Prompt, you can find it of off the Start button on the taskbar on Win 10 that shows the list of installed software most likely, as an example of how you can get to the VS Command Prompt. 

    • Marked as answer by MyCatAlex Thursday, November 21, 2019 2:18 AM
    Wednesday, November 20, 2019 11:16 PM

All replies

  • It seems to me the system suggests a solution but I will have to mess with the Registry.

    .NET DLL(s) do not need to be registered with the O/S. So that can't be the problem, becuase .NET looks for the DLL in the same location as the programname.exe file.

    The version of the file in both folders is different,

    The program assembly manifest is looking for a particular version of the DLL and it is not there in the folder.


    • Edited by DA924x Tuesday, November 19, 2019 10:23 PM
    Tuesday, November 19, 2019 10:23 PM
  • Try (if you have not) using the NuGet package rather than the DLL's installed on your computer. This means there is only one version available.

    https://www.nuget.org/packages/Unofficial.Microsoft.SQLServer.SMO.2014/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 19, 2019 11:19 PM
    Moderator
  • It seems to me the system suggests a solution but I will have to mess with the Registry.

    .NET DLL(s) do not need to be registered with the O/S. So that can't be the problem, becuase .NET looks for the DLL in the same location as the programname.exe file.

    The version of the file in both folders is different,

    The program assembly manifest is looking for a particular version of the DLL and it is not there in the folder. What shall I do then?


    The versions are different in my Debug and Release folders versus what the Exception expects. What shall I do then, and why it works for Debug but not the Release. This older version works fine for the Release version.

    Thanks, - MyCatAlex

    Wednesday, November 20, 2019 12:10 AM
  • The versions are different in my Debug and Release folders versus what the Exception expects. What shall I do then, and why it works for Debug but not the Release. This older version works fine for the Release version.

    Well do a Clean on the project in release mode using Visual Stuido, go back to Debug mode and use Nuget to get the most recent version, build,  see if that corrects the problem when you go back to Release mode.

    Myself, I have never used the release mode.

    Wednesday, November 20, 2019 1:23 AM
  • Try (if you have not) using the NuGet package rather than the DLL's installed on your computer. This means there is only one version available.

    https://www.nuget.org/packages/Unofficial.Microsoft.SQLServer.SMO.2014/



    It gave me an error. The name "paket" is not recognized.

    - MyCatAlex

    Wednesday, November 20, 2019 4:51 PM
  • Try (if you have not) using the NuGet package rather than the DLL's installed on your computer. This means there is only one version available.

    https://www.nuget.org/packages/Unofficial.Microsoft.SQLServer.SMO.2014/



    It gave me an error. The name "paket" is not recognized.

    - MyCatAlex

    Where did you get the error on NuGet or in Visual Studio?

    You should be able to see it in Visual Studio by using the search on the package page.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 20, 2019 5:07 PM
    Moderator
  • To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

    [...]

    It seems to me the system suggests a solution but I will have to mess with the Registry.

    You don't actually need to mess with the Registry. Visual Studio comes with a little utility called FUSLOGVW.EXE (that's short for "Fusion Log viewer")that you can invoke from a Visual Studio Command Prompt. Run it as Administrator or else most of its options will be grayed-out. From this tool there is a button for enabling the Log. Internally, it edits the registry like the error message told you, but in this way you don't need to mess around with the registry; the tool knows what to change.

    You can then run the program until it throws the error. Then, you press "Refresh" in FUSLOGVW and it shows the errors that were logged. You double-click on one of the errors, and it opens a window that displays what the program was looking for, versions, places where it looked and why it wasn't found. It's useful for knowing why the version of your assembly cannot be used. Most likely the program is looking for a different version and you don't have a bindingRedirect in the .config file that allows this particular version substitution, while it does allow the one in your Debug folder.

    Wednesday, November 20, 2019 7:43 PM
    Moderator
  • To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

    [...]

    It seems to me the system suggests a solution but I will have to mess with the Registry.

    You don't actually need to mess with the Registry. Visual Studio comes with a little utility called FUSLOGVW.EXE (that's short for "Fusion Log viewer")that you can invoke from a Visual Studio Command Prompt. Run it as Administrator or else most of its options will be grayed-out. From this tool there is a button for enabling the Log. Internally, it edits the registry like the error message told you, but in this way you don't need to mess around with the registry; the tool knows what to change.

    You can then run the program until it throws the error. Then, you press "Refresh" in FUSLOGVW and it shows the errors that were logged. You double-click on one of the errors, and it opens a window that displays what the program was looking for, versions, places where it looked and why it wasn't found. It's useful for knowing why the version of your assembly cannot be used. Most likely the program is looking for a different version and you don't have a bindingRedirect in the .config file that allows this particular version substitution, while it does allow the one in your Debug folder.

    Alberto hi,

    I will do as you suggested but in the meantime I did my own research. BTW I am not averse to go to the Registry and change things but I believe it is the last resort. My research showed that I installed Nuget perhaps 2 years ago and I have a folder for it. Even in Nuget Folder there is no smo.dll file with this version. I also searched entire C: disk and found a bunch of Smo.dll's but ALL the versions are older. HOWEVER when I open the Visual Studio (2017) and open my Solution and check Solution Explorer==>References and check the version of the Microsoft.SqlServer.Smo.dll file referred there and go to the Properties, it says that the referenced version of that file is 14.100.0.0 which is exactly what the Exception wants. There is also one more line in the properties. That line says:

    Runtime Version v2.0.50727

    Obviously it is accessible only to Debug version, as I understand it. I also followed the path that points to this Smo.dll file and checked the version of that file. Surprise, surprise the version of Smo.dll file in that folder which is C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Tasks\Microsoft.SqlServer.Smo.dll is NOT 14.100.0.0. It is 14.1.17265.0, There is a huge discrepancy.

    I am confused. I will do what you suggested but that will be my last act. In principle, I can work with the debug version with the Visual Studio open for a while.

    Thank you, - MyCatAlex

    Wednesday, November 20, 2019 10:03 PM
  • Alberto hello,

    I cannot find Visual Studio Command Prompt. There is Package Manager Console. I tried to run FUSLOGVW.EXE in it and got an error that the cmdlet is not recognized. I have a developer version of VS. Could it be a problem?

    Thanks, - MyCatAlex

    Wednesday, November 20, 2019 10:26 PM
  • I am confused. I will do what you suggested but that will be my last act. In principle, I can work with the debug version with the Visual Studio open for a while.

    I have never used Release mode in VS not in any professional or home usage environment. :) And I have used all the versions of VS offering the feature.

    And about VS Command Prompt, you can find it of off the Start button on the taskbar on Win 10 that shows the list of installed software most likely, as an example of how you can get to the VS Command Prompt. 

    • Marked as answer by MyCatAlex Thursday, November 21, 2019 2:18 AM
    Wednesday, November 20, 2019 11:16 PM
  • I found it, thanks. - MyCarAlex
    Thursday, November 21, 2019 2:19 AM