ASP.NET MSSQL fails to connect via web app, but succeeds in Server Explorer - asp.net

Tech used:
Visual Studio 2015
ASP.NET 5 MVC 6 RC1
Identity via EF 7
I've successfully made a connection to a remote MSSQL server using the Server Explorer UI within VS2015. When I say "remote", I mean not on the same machine as my dev machine, but a machine that is local to the network.
Here is the connection string, which is the same in the Server Explorer (works) and the appsettings.json (doesn't work):
"Data Source=DBSERVER\INSTANCE1;Initial Catalog=MyDb;User ID=ASPNET-Db-User;Password=thepassword"
I'm using SQL Authentication in both cases.
When I copy the connection string from the server explorer and plug it into appsettings.json and try to connect to the db in order to create an Identity user account via the "register account" view, I get a connection error:
SqlException: 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
It seems that, clearly, the SQL instance is reachable, but somehow ASP.NET isn't able to connect to it.
Any idea how I might trouble shoot this?

I tried the same code in MVC5 and everything works as expected. I believe this to be a MVC 6 error and have opened a bug report at
https://github.com/dotnet/corefx/issues/4586

After chatting with the folks building MVC 6, it turns out that the CoreCLR does not support named pipes. Using a fully qualified address and an explicit port sorts the problem.

Related

.NET Core API cannot connect to MS LocalDB

I am writing a .NET Core 2.1 web API using MS LocalDB. I can connect to my DB fine using SSMS and the server name (localdb)\MSSQLLocalDB with Windows Authentication.
The API fails to connect with the error:
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: SQL
Network Interfaces, error: 50 - Local Database Runtime error occurred.
Cannot create an automatic instance. See the Windows Application event
log for error details.
My connection string is:
Server=(localdb)\\MSSQLLocalDB;Database=Foo;Trusted_Connection=True;MultipleActiveResultSets=true
As a side note, I tried to check my LocalDB version with sqllocaldb v but got the following error:
Windows API call "RegGetValueW" returned error code: 0.
Is my LocalDB installation completely borked?
its kinda late but i am trying this for 2 days and finally found the answer.
When app is running on IIS
--> go to advance setting in the application pool, then on change "Process Model->Load Profile" to TRUE;
when app is running on Kestrel,
a. connection string "Data Source=(localdb)\mssqllocaldb"... etc
hope this help the others.

ASP.net can't connect to SQL Server after being deployed to hosting site

I'm developing an ASP.net web application.
My default page is a simple login screen.
I am using Visual Studio to develop the application.
It will run using Visual Studio and when the data is entered to the text boxes and the button is clicked, it sends the request to my own server running Windows Server 2012 with SQL Server 2014 installed.
I will also mention that the ASP application is being created as a secondary application for an Android application. The Android application is using the same database over Internet connection so I know that the server and SQL server both can accept read and write calls.
I have tested the ASP application using WebClient calls to php files which connect to the database (this works on Android) and also OracleClient and SQLClient calls all of which work from Visual Studio on my workstation to the server machine. These all work fine to send and receive data from the database.
The problem is once I deploy the application to my hosting site the calls no longer seem to work. The default login page opens but after attempting to login I receive errors as follows:
Exception Details: System.ComponentModel.Win32Exception: No connection could be made because the target machine actively refused it
[Win32Exception (0x80004005): No connection could be made because the target machine actively refused it]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
Can anyone help me with this?
It sounds like your database is accepting connections from some clients, but not your hosting server. I would try testing a SQL connection straight from the hosting Server and see if a test connection can be successful. If you can get that to work it should work from the web server.
This article Quick ways to test OLE DB Connection String on creating a empty TestConnection.udl file will be helpful. It is easier to try different connection string settings through the Data Link Properties UI. When you save the file, you can see the connection string if you open it in a text editor (assuming it is successful).
If it's not a problem with he connection string, check that you do not have a firewall blocking connections between the Windows 2012 and SQL servers. It could be the widows firewall on either machine, or another firewall at the network level.

Error inserting/deleting records ASP.NET web application deployed on Windows server 2012

I developed an ASP.NET web application and it worked fine locally with IIS express.
I created deployment package and installed the application on my Windows 2012 server.
When developing the application, I was using an mdf file with localdb v11.0. On the server, however, I wanted to use sqlserver express. I changed the connection string to
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\nameoffile.mdf;Integrated Security=True;Connect Timeout=30
After making this change I was able to connect to the application from another host on the network. Viewing data and using filters/search (built into stored procedures) works fine.
The error occurs when I try to insert new records or delete records. The message is:
Server Error in '/Nameofapplication' Application.
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: SQL
Network Interfaces, error: 50 - Local Database Runtime error occurred.
Cannot create an automatic instance. See the Windows Application event
log for error details. )
I've turned off the firewall, allowed remote connections, started sql server browser and agent and various other solutions that I've found from googling without any luck.
Any ideas about how to solve this issue?

Asp.Net With SQL Server And Asp.net Linq Application

I have an application that uses linq-to-sql with Sql Server Express 2008. When I use the DBML file in a separate DAL project and refer to it to my web project, on execute Sql Server shows this error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
But when I use the DBML in my web project, debugging is without any problem. But on IIS in Windows 7 64bit i get:
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)
Do these two errors have the same cause? Is the connection string not correct?
I'd suspect that in the first case you have the connection string in the DAL project and not in your web project (where it belongs). Make sure you copy it to the web.config of your web project.
In the second case I think that that the machine hosting IIS tries to connect to the database on your own machine. If so, check the exception: make sure your SQL Express instance is configured to allow remote connections, that the IP protocol is enabled (named pipes if for local traffic), and that your machine is accessible to the IIS machine.
I think, Your connections string is wrong.
dont worry.
check your dbml's designer.cs file. in which you will find '<YourDBMLFile>DataContext()' Constructor.
and Change your Connection string. make sure your connectionstring is correct.

ASP.NET Membership DB connection

I can neither login from my application nor connect using the wizard to my Membership Provider on my local machine. My other DBs can connect from my application just fine. My connection string is correct, it has not been changed (I was able to login using membership previously, this is a new issue).
I created a new App to see if it could be the config, threw a login on it and registered and ran into the same issue.
This is the error message I am getting: "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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) "
Though my other DB can connect I still checked:
TCP/IP enabled
Multiple connections enabled
Firewall down
Correct instance of sql server being referred to in Machine config.
Also, my network service account has rights to the DB, but should there be an ASPNET user?
I can't think of a single thing I have changed to this machine since this was working previously.
Thank you.
EDIT DETAILS: There are no aliases, yet, but if my other connections are working that shouldn't be the issue. I understand these things can be wonky though, so that's something I'm going to try now. Thanks. I will also try and take a look at what's going on while trying to connect. Best way: event logs?
I am using Windows Auth, I'm connecting locally, using Windows 7 and SQL Server 2008 along with VS 2008. I can connect with Management Studio and query the DB, yes. NetworkService account has rights to the DB.
Check the Server name in the connection-string if the Sql-Server is in the same machine with the web application use "."(dot) without quotes instead of the computer-name in your connection string.
Have you tried sniffing it to see if there's anything you can see on the wire when the connection attempt is made?
Are there any aliases around the server/DB name? Have you tried creating one and connecting to it?
Are the other instances you can connect to on the same machine? Same SQL Server instance?
If you use impersonation, does the problem go away?
It sounds like you can connect to one database, but not the one you need. Is that correct?
The ASPNET user is a Windows XP thing. You should generally use NETWORK SERVICE for server-based deployments.
So, you can connect from Management Studio, but not from IIS? What identity are you using for the IIS AppPool?
Can you connect if you use Cassini (the web server that comes with Visual Studio)? That should pass through your user ID.

Resources