1

I know there are a lot of similar questions out there. The last days I've spent my time going through the answers to the question and tried everything, that was suggested. But nothing worked, which is why I am putting my question out there, hoping someone has an idea.

I've got a console application and a web application, both running on the same server. Both of them connect to a SQLExpress Database, which is also on the same server. The web application running on IIS was able to connect to the database and can read and write data. But the console application can't. This is the Error-Message i'm getting:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) System.ComponentModel.Win32Exception (0x80004005): The network path was not found

bei 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)
   bei System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   bei System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   bei System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   bei System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   bei System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   bei System.Data.SqlClient.SqlConnection.Open()
   bei System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<>c.<Open>b__13_0(DbConnection t, DbConnectionInterceptionContext c)
   bei System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   bei System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   bei System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass60_0.<UsingConnection>b__0()
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass2_0.<Execute>b__0()
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   bei System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act)
   bei System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)
   bei System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
   bei System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   bei System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   bei System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
   bei System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
   bei System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   bei System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   bei System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
   bei System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState)
   bei System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
   bei System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
   bei System.Data.Entity.Internal.InternalContext.<>c__DisplayClass66_0`1.<CreateInitializationAction>b__0()
   bei System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   bei System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   bei System.Data.Entity.Internal.LazyInternalContext.<>c.<InitializeDatabase>b__58_0(InternalContext c)
   bei System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   bei System.Data.Entity.Internal.InternalContext.Initialize()
   bei System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   bei System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   bei System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   bei System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   bei System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)

ClientConnectionId:00000000-0000-0000-0000-000000000000 Error Number:53, State:0, Class:20

The connectionstring is the same in both projects:

<connectionStrings>
    <add name="DbContextModel" connectionString="data source=DESKTOP\SQLEXPRESS;initial catalog=ApplicationDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>

I already tried and checked a lot of things, including:

  • checking all the SQL Services are running
  • Named Pipes, TCP is activated in the SQL Configuration Manager
  • I tried turning Antivirus and Firewall off -> no difference
  • I added the User to the Login of the database as a user, which is used when the console app is started
  • I tried by changing the connectionstring
  • I checked and inserted the tcp port 1433 in the SQL Configuration Manager
  • I checked that SQL allows remote connections
  • I checked and made sure that SQL uses the port 1433 in case of tcp

I'm at a point where I've no idea what I should do or try next next. And I don't understand why the web application has no problem using the database, but the console application can't. So I'd really appreciate any help. Thank you very much.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • change "source=DESKTOP\SQLEXPRESS;" to "source=.\SQLEXPRESS;" – Ehsan Kiani Aug 31 '21 at 07:51
  • Is the connection string in the console app's App.config? – Gert Arnold Aug 31 '21 at 07:56
  • changin "source=DESKTOP\SQLEXPRESS;" to "source=.\SQLEXPRESS;" didn't resolve the issue. – happyCoding Aug 31 '21 at 08:51
  • And yes the connection string is in the console's App.config – happyCoding Aug 31 '21 at 08:52
  • 2
    When you have eliminated the impossible, whatever remains, however improbable, must be the truth. Double check the configuration of the web application (in IIS itself, do not simply rely on local config files) to see what exact connection string it's using that works (if you can afford it, restart it to confirm it remains operational), and double check (with breakpoints or logging) that the connection string in your console's config is the actual string being used (a cheap way of doing it is making the string invalid, like `xxx`, and seeing it if barfs at the same point). – Jeroen Mostert Aug 31 '21 at 09:52
  • When you changed the port to 1433 did you restart the SQL Server service for the changes to take effect? If you know it's running on port 1433 why are you using the `\SQLEXPRESS` instance name - why not use `localhost,1433` instead? Instance name resolution requires the SQL Browser service to be running and firewall-accessible at udp/1434. – AlwaysLearning Aug 31 '21 at 10:48
  • @JeroenMostert Thank you so so much for the tipp! I found the problem. – happyCoding Aug 31 '21 at 11:37
  • @JeroenMostert Do you want to post your tipp so I can accept it? – happyCoding Aug 31 '21 at 11:46
  • The tip could not qualify as an answer, I think. SO is a bad fit for "help me debug this code" questions if it's unclear from the question what the problem is. If you've actually found the problem, feel free to post an answer yourself (don't post it in the question), that might help someone out. – Jeroen Mostert Aug 31 '21 at 11:49

1 Answers1

1

I got the tipp to double check the connection string and if my application really took the connection string of the config file. So I double checked and noticed that by writing an invalid connection string in the config file, the error message didn't change. The reason for that was, that for whatever reason there was another config file in bin/Debug called Projectname.config. I guess Visual Studio put it there. The application was reading an older connection string from that config file, which caused the problem.