Tomas Aschan
Tomas Aschan

Reputation: 60634

"A network-related or instance-specific error occured" - but for which connectionstring?

When trying to start debugging my application, I'm seeing a (very common) System.Data.SqlClient.SqlException about "a network-related or instance-specific error" that apparently occurred.

I've understood that this means that there's a problem with my connection string, but I have quite a lot of different connectionstrings in my config files, that are used in different configurations etc, so I'm at a loss at knowing which one is giving me trouble.

Is there any way to find the connection string in use when this error is thrown?

Upvotes: 0

Views: 124

Answers (3)

Leo
Leo

Reputation: 14850

Not necessarily a connection string problem but a failure to connect to a data source...e.g. a network connection issue

1

The most straight-forward thing to do is to debug the application. When the exception is thrown you can easily traverse the call stack up until the call to SqlConnection.Open where you will be able to see the whole connection string. That's if you're using the System.Data.SqlClient provider's classes directly

2

If you're using an ORM tool, it might be slightly more difficult and you will need to refer to the documentation of that specific tool in order to be able to output debug and/or tracing info

Wish I could be more specific, but the steps depends on how you have your data access logic set up because it varies a lot from one approach to another

Example

I went in and opened a project that uses Fluent NHibernate and intentionally created a bug in the entity mappings below by misspelling the table name...

internal class RiskLevelDOMap : ClassMap<RiskLevelDO>
{
    internal RiskLevelDOMap()
    {
        Id(x => x.Id);

        Map(x => x.MatrixId, "RiskMatrixId");
        Map(x => x.DisplayText).Length(256);
        Map(x => x.Description).Nullable().Length(2000);
        Map(x => x.BackgroundColor).Nullable().Length(64);
        Map(x => x.ForeColor).Nullable().Length(64);
        Map(x => x.DisplayInReports);
        Map(x => x.IsActive);

        Table("rm_RiskLevel1");
    }
}

ran the debugger and see the screenshot attached below...the connection string in plain text...

enter image description here

However, it's worth noting that I do have a pretty well-structure data access implementation where I'm spitting out a lot of debugging information

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

One approach may be logging ClientConnectionIds. These are available on both SqlConnection and SqlException, assuming that the connection has been established.

  • When connection is opened, log your connection string, along with the ClientConnectionId of your connection
  • When you get an exception, log ClientConnectionId
  • To find what connection string corresponds to DB that caused the exception, search through the log for the ClientConnectionId that you logged when establishing the connection.

Upvotes: 0

Schwarzie2478
Schwarzie2478

Reputation: 2276

In the exceptions dialog of Debug menu( Visual Studio), you can enable the debugger to stop on this error( System.Data.SqlClient.SqlException) specifically, then you could go up the call stack and see which string is being used...

Upvotes: 0

Related Questions