Ed Landau
Ed Landau

Reputation: 998

"The network path was not found" / "Named Pipes Provider, error 40" when running from the bin directory. Works from VS

I am running a C# .NET 4.5 console app. My configuration file looks like this:

<connectionStrings>
    <add name="DataContext" connectionString="data source=1.2.3.4;initial catalog=db1;user id=Jon;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
 </connectionStrings>

My SQL interface is:

public static DataTable GetDataTableSql(string SqlCommand)
{
    DataSet _data = new DataSet();
    try
    {
        using (SqlConnection _connection = new SqlConnection(s_connectionString))
        {
            if (_connection.State != ConnectionState.Open)
            {
                _connection.Open();
            }
            var _cmd = new SqlCommand(SqlCommand, _connection) {CommandType = CommandType.Text};
            //SqlDataAdapter sda = null;
            using (SqlDataAdapter _sda = new SqlDataAdapter(_cmd))
            {
                _sda.Fill(_data, "data");
            }

            _connection.Close();
            _connection.Dispose();
        }
    }
    catch (Exception _e)
    {
        s_logger.Error("GetDataTableSQL failed: {0}, {1}", SqlCommand, _e.Message);
    }
    if (_data != null && _data.Tables != null && _data.Tables.Count > 0)
    {
        return _data.Tables[0];
    }
    return null;
}

This works beautifully when I start from Visual Studio. 100% of the time.

However, if I go to bin/Debug and run the .exe, I get an error

The inner exception in the catch statement below is: "the network path was not found"

Here's the interesting part: if I set a breakpoint on the using and step through IT WORKS (note I run the process from bin/Debug and then Attach to the process with VS). Then when I remove the breakpoint IT WORKS. If I Rebuild, IT STILL WORKS. BUT... if I Clean then Rebuild, it fails again.

So:

I'm so confused :).

Help help would be appreciated.

Thanks -Ed

Upvotes: 1

Views: 959

Answers (1)

Ed Landau
Ed Landau

Reputation: 998

This was the strangest issue since the code worked when run from within VS, just not when running the .exe manually from the bin/Debug directory. Stranger still is that if I used VS to attach to the manually run .exe and stepped thru the sql .open call, it failed but would then work thereafter until I did a clean / rebuild. Made absolutely no sense.

Then I saw on other Stackoverflow posts that the antivirus / firewall may have something to do with it. It did. Dissabling BitDefender's firewall fixed my issue. I still do not fully understand the behavior... but I can clearly see the correlation and it is 100%. Dissable the firewall, it works. Enable it and it fails. period. Strange. 'Hope this helps at least one person out there!

-Ed

Upvotes: 1

Related Questions