0

TL;DR

WinForms in a C# DLL using DataBinding to a data source appear to be blocked from accessing the SQL Server database, yet raw SQL queries work. What am I doing wrong?


Detailed Problem Description

We have a C# WinForms .Net Framework 4 application consisting of: - An EXE Project - A DLL Project, called by the EXE

The application accesses an SQL Server database.

The EXE and the DLL both contain some Windows Forms created using the Visual Studio designer. To show data in the form controls, the controls were added to the form via drag-and-drop from the "Data Sources" panel. i.e. data access goes through an dataset stored (in the Visual Studio project) as an XSD file.

On the development machine everything works OK, and we can open the various forms and see the data. But on the test system, where SQL Server is also installed and running, only the forms in the EXE can access the database. If we open a form from the DLL, we get the app hangs for a few seconds, then comes the error:

System.Data.SqlClient.SqlException (0x80131904): Cannot open database "MyDatabaseName" requested by the login. The login failed. Login failed for user '\'.

at the call:

System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)

The connection string in app.config is correct, and the app is already connected to the DB because raw SQL queries/updates work OK.

The strange thing is that, if I go into VS and recreate the data source (using the Wizard) and instead use the database on the test machine instead of the development machine, and then recompile, then everything works on the test machine. It feels almost like the parameters in the connection string are being "compiled" into the DLL somewhere, but so far I can't figure out what is wrong.

We're using SQL Server 2016 and Visual Studio 2013. The app is compiled for .Net 4.0.

Connection string in use:

Data Source=192.168.0.1;Initial Catalog=MyDatabaseName;Integrated Security=True

Have also tried:

Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True

Sorry for the long-winded explanation (thanks for reading this far!) but it does seem curious. Does anyone know what is going on here? Thanks in advance for any hints!

1 Answer 1

0

OK, I have finally got this to work, and this link was particularly helpful:

http://jstawski.com/post/2007/01/24/connectionstring-on-a-dataset-datatable-with-dataadapter

In my projects, I changed the connection string. Instead of (localdb) as my server, I entered the IP of another machine (IP 192.168.0.224) on which SQL Server is configured. I was able to keep debugging my program in the IDE.

When I deployed my app to a different machine (IP 192.168.0.35), all raw SQL queries worked on the database for Nr 35. But anything involving the XSD dataset changed the data on the other PC (192.168.0.224) despite my connection strings in the app.config being set for 192.168.0.35!

When you create a dataset for your project using the VS Wizard, you are asked for a connection string at the beginning of the wizard. While all works as it should do when the project is an EXE, if you are compiling a DLL (as I am), it seems that this connection string (as it stands on the development PC) is somehow hard-coded into the DLL.

Solution

The DLL project has a Settings file in which the connection string can be set. The solution was to trick VS by overriding this setting.

My original file:

    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=192.168.0.224;Initial Catalog=My_Database_Development;Integrated" +
            " Security=True")]
        public string MyDatabaseConnectionString {
            get {
                return ((string)(this["MyDatabaseConnectionString"]));
            }
        }
    } 

The changed part was as follows:

       public string MyDatabaseConnectionString {
            get {
                //return ((string)(this["MyDatabaseConnectionString"])); // <-- this is the cause of the trouble!
                return getConnectionString();
            }
        }

        private string getConnectionString()
        {
            // This is a method I wrote elsewhere in my program which always queries the app.config file
            return ConnectionStringUtility.GetConnectionString();
        }

Note: I have in my program a utility class containing a method that returns the connection string, so I was able to just call my utility class here in the Settings.Designer.cs

Summary

It's a very strange situation that only occurs when compiling a C# project containing a DataSet (XSD) to DLL.

The solution was to override the call to get the connection string in Settings.designer.cs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.