C# SqlConnection fails in one solution but not in another with identical code RRS feed

  • Question

  • I asked this on another site and I may as well have put my head in an oil-well burn-off fire.  If this question generates as much displeasure and carries as much uselessness as was deemed on another site, I'm sorry in advance for asking.  I'm just seeking advice.  

    I have a C# project built out with numerous namespaces and classes.  Nothing remarkable.  When I added classes to manage SQL connectivity and functions, the connectivity failed every time.  It would get to the Open method and sit, eventually failing.  I create a new project, insert the same code, and it works.  Both projects on the same machine, same OS, same VS version, at the same time.  I can take the code from the working project, paste it in Main() in the project that was failing to begin with, and it still fails with the same behavior.  I placed it in Main() in case there could be namespace issues, or some sort of scope problem I'm missing.  

    My question, is, if I have checked all project settings, environment options, etc. ... cleared the bin and obj folders, cleaned, rebuilt ... what would cause one project to act as if the connections were blocked, but yet the identical code works fine in a new project???

    The Exception message:

    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)

    The code:

                    string s = "Data Source=SERVER;Initial Catalog=DB;Trusted_Connection=True;";
                    SqlConnection oCN = new SqlConnection(s);

    • Edited by APPDVLPR Wednesday, February 13, 2019 10:03 PM
    Wednesday, February 13, 2019 9:55 PM

All replies

  • Yeah, what O/S(s) are invloved here on client machine and  database server?
    Wednesday, February 13, 2019 10:09 PM
  • You can check if the specific server is available on the current machine attempting to access data using the following library.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 14, 2019 1:37 AM

    Did you ever get this figured out? I have the same exact problem and have gone through both solutions with a fine toothed comb and cannot see a difference but yet one will work and the other will fail.

    Barry Kaufman

    Tuesday, June 25, 2019 10:11 PM