0

Whenever I debug my ASP.NET Web Form, I get the following error:

System.Data.SqlClient.SqlException occurred HResult=0x80131904
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)
Source=.Net SqlClient Data Provider
StackTrace:
<Cannot evaluate the exception stack trace>

Inner Exception 1:
Win32Exception: The network name cannot be found

I researched a lot of stuff relating to error 40, and tried everything recommended. Enabled TCP/IP, set the port to 1433, created firewall rules allowing connections through TCP/IP port 1433, restarted the server after each change, and then restarted the computer. Basically, everything from this thread. The SQL Server is on the same machine that I am testing from, so it's not a remote connection issue, I wouldn't think. There is the Inner Exception about Win32Exception, but everything I'm running is 64bit. Maybe this has something to do with it?

Running Windows 10, Visual Studio 2017 Community Edition, SQL Server 2016 Express, and the relevant code is below:

String IacNum = txtIacNum.Text; String CheckResult;

    String queryString = "SELECT ExpirationDate FROM [dbo].[IACList] WHERE ApprovalNumber = @IacNum";
    String connectionString = "Data Source=PCNAME/SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";

    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand command = new SqlCommand(queryString, connection);
    command.Parameters.AddWithValue("@IacNum", IacNum);
    command.Connection.Open(); //Exception pops up here
    var reader = command.ExecuteScalar();
    if(reader != null)
    {
        CheckResult = "Valid. Expires" + reader;
    }
    else
    {
        CheckResult = "Invalid";
    }
    command.Connection.Close();
        MessageBox.Show(this, CheckResult);
Community
  • 1
  • 1
Josh Eblin
  • 95
  • 1
  • 9
  • 1
    Maybe it's a typo but please double check you 're using the right slash (back slash) instead of forward slash here `Data Source=PCNAME\SQLEXPRESS;In...` – Karel Tamayo May 02 '17 at 20:03
  • 1
    @KarelTamayo that was the problem. However, the reason was that when I copied the connection string, I initially got a red error underneath the back slash (\), and it said Unescaped error sequence, and for some reason my first thought was to change the \ to /. Changed it instead to \\ (escaping the back slash). Thank you. – Josh Eblin May 02 '17 at 20:36

1 Answers1

1

Try viewing the SQL Server from Server Explorer\Data Connections. If you can create a connection, you will see a connection string property. You can than compare that and see if you the same values -- you may have a typo.

Kevin Raffay
  • 842
  • 5
  • 18
  • I copied the connection string from the properties of the Data Connection. the connection string showed "Data Source=PCNAME\SQLEXPRESS;Initial Catalog=TASdb;Integrated Security=True", but I get a red error line underneath the \, stating that there is an Unrecognized escape sequence. now changed it to \\ to escape the back slash. Thanks, Kevin. – Josh Eblin May 02 '17 at 20:37