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);