52

Well i have a big problem that i'm trying for days to solve but i could not do it, so i need your help. I have a web application in asp.net 4.0 where i implemented custom membership and role providers. When i open the app homepage in it connects to the database and retrieves some information. There is also a login form and when i login it validates the user through the database and redirects me to the reserved pages but than this error appears:

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)

Description:

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:

System.Data.SqlClient.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)

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I tried many things, some solution i found in stackoverflow but nothing worked. Here are my connectionstrings:

<connectionStrings>
  <clear/>
  <remove name="LocalSqlServer"/>
     <add name="LocalSqlServer" connectionString="Data Source=MSSQL2008-1;Initial Catalog=agency;
        Integrated Security=False;User ID=adis_agency;Password=niarda2012;Connect Timeout=180;Encrypt=False;
        Packet Size=4096" providerName="System.Data.SqlClient" />
     <add name="AgencyConn" connectionString="Data Source=MSSQL2008-1;Initial Catalog=agency;
        Integrated Security=False;User ID=adis_agency;Password=niarda2012;Connect Timeout=180;Encrypt=False;
        Packet Size=4096" providerName="System.Data.SqlClient"/>
</connectionStrings>

Below are also the membership and role manager web.config section:

<membership defaultProvider="AuthProvider">
  <providers>
    <clear/>
      <add name="AuthProvider" type="AuthenticationProvider" applicationName="~/Reserved/" connectionStringName="AgencyConn"/>
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="MyRoleProvider">
  <providers>
    <clear/>
      <add name="MyRoleProvider" type="RoleAccessProvider" connectionStringName="AgencyConn"/>
  </providers>
</roleManager>

Please can anybody help to figure out whats wrong?

Pedram
  • 6,256
  • 10
  • 65
  • 87
ivi.hamiti
  • 552
  • 1
  • 4
  • 7
  • Check all connectionStrings in your project. It may not be only one connectionString that you might be seeing and which is correct connectionString. It might be another connectionString that might be incorrect. "check all connectionStrings" in your project. – Jogi May 03 '16 at 10:54
  • I had the same issue, For me, the issue was My SQL server instance was listening on some different port and I had not specified the port number in my connection string. connectionstring=server= databaseserver,port – Sachin Vishwakarma Aug 07 '19 at 09:59
  • Please see here [Error Locating Server/Instance Specified](https://blogs.msdn.microsoft.com/sql_protocols/2007/05/13/sql-network-interfaces-error-26-error-locating-serverinstance-specified/) – Andile Sep 13 '19 at 07:11
  • HATE the way SO close this type of question. It's a programmer having a problem that lots of other programmers have experienced. SO don't want the person to get any help! – Paul McCarthy Apr 21 '23 at 11:51

19 Answers19

55

If you are connecting from Windows machine A to Windows machine B (server with SQL Server installed), and are getting this error, you need to do the following:

On machine B:

1.) turn on the Windows service called "SQL Server Browser" and start the service

2.) in the Windows firewall, enable incoming port UDP 1434 (in case SQL Server Management Studio on machine A is connecting or a program on machine A is connecting)

3.) in the Windows firewall, enable incoming port TCP 1433 (in case there is a telnet connection)

4.) in SQL Server Configuration Manager, enable TCP/IP protocol for port 1433

enter image description here

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • 1
    Only could access after setting the tcp port under IPAll section! If I set it for specific IP only and keep dynamic port blank and make `Enabled:Yes` it didn't connect! this can be confirmed by `netstat -ano | find ":port"` in which sql server only listen if the port being set under IPAll section. – Jawad Al Shaikh Feb 05 '18 at 18:46
  • What OS and version are you on @JawadAIShikh? – JustBeingHelpful Feb 05 '18 at 19:15
  • 1
    Server 2012 R2 Standard, and SQL Server 2012 express with tools. Btw, my previous comment meant to be a complement to your answer since MS official docs didn't mention to set the port under IPAll: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-a-server-to-listen-on-a-specific-tcp-port – Jawad Al Shaikh Feb 05 '18 at 19:43
  • 1
    The issue for me was *turn on the Windows service called "SQL Server Browser" and start the service* – Gerhard Apr 18 '18 at 08:10
  • @Gerhard - you mean that was your solution? – JustBeingHelpful Apr 25 '18 at 22:29
  • As a work around (after much time spend) I used ip address\instance and it worked. When I use the computer name it fails. – JohnC Aug 09 '20 at 17:23
  • This article is fantastic also! https://social.technet.microsoft.com/wiki/contents/articles/2102.how-to-troubleshoot-connecting-to-the-sql-server-database-engine.aspx – JustBeingHelpful Dec 15 '20 at 01:46
22

For me the issue was that the DNS record was wrong...

The following, which proved very helpful, is largely taken from this blog post.

This error message is actually pretty specific and the solution quite simple.

You get this error message only if you are trying to connect to a SQL Server named instance. For a default instance, you never see this because even if we failed at this stage (i.e. error locating server/instance specified), we will continue to try connect using default values, e.g default TCP port 1433, default pipe name for Named Pipes.

Every time a client makes a connection to SQL Server named instance, we will send a SSRP UDP packet to the server machine UDP port 1434. We need this step to know configuration information of the SQL instance, e.g., protocols enabled, TCP port, pipe name etc. Without this information the client does not know how to connect and it fails with this error message.

In a word, the reason that we get this error message is the client stack could not receive SSRP response UDP packet from SQL Browser. In order to isolate the exact issue follow these steps:

  1. Make sure your server name is correct, e.g., no typo on the name.

  2. Make sure your instance name is correct and there is actually such an instance on your target machine. (Be aware that some applications convert \ to ).

  3. Make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true).

  4. Make sure the SQL Browser service is running on the server.

  5. If the firewall is enabled on the server, you need to put sqlbrowser.exe and/or UDP port 1434 into exception.

There is one corner case where you may still fail after you checked steps 1 to 4. It also may happen when:

  1. your server is a named instance on cluster or on a multi-homed machine
  2. your client is a Vista machine with Firewall on.

A tool which could prove useful (it did for me) is PortQry. If this command returns information and it contains your target instance, then you can rule out possiblity 4) and 5) above, meaning you do have a SQL Browser running and your firewall does not block SQL Browser UDP packet. In this case, you can check other possible issues such as an incorrect connection string.

As a final note, the error message for the same issue when you use SNAC is: [SQL Native Client]SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].

Microsoft recently released a guided walk through that can serve as a one stop shop to troubleshoot a majority of connectivity issues to SQL Server: Solving Connectivity errors to SQL Server

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • 1
    lol. In our instance, our organization installed a new instance on a new machine and moved all the old DB's. Now this was on our Dev box. For some reason, our DBA decided it would be a good idea to reference something in our dev box directly from our live DB, thus it wouldn't have anything to do with a connection string from our application, however it comes down to the fact that the old server instance could not be found. You nudged me into the right direction here. Thank you – Gawie Schneider Jul 03 '17 at 09:02
  • 1
    Opening UDP port 1434 Worked for me. Thanks. – QMaster May 15 '19 at 16:33
  • Very useful notice about PortQry – Kostia Shiian Jan 27 '21 at 19:29
11

Goto window+R and type services.msc and press enter.

Then start SQL server manually if not automatically started.

Then try to login.

Laura White
  • 188
  • 2
  • 9
Venkatesh K
  • 145
  • 2
  • 5
8

I had been experiencing the same problem, in my ASP.NET MVC 4 application.

The way I solved it, was in the DatabaseContext. By passing down the name of the connection string I wanted to use through the base constructor.

public class DatabaseContext : DbContext
{ 
    public DatabaseContext()
        : base("DefaultConnection") // <-- this is what i added.
    {
    }

    public DbSet<SomeModel> SomeModels { get; set; }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Kristian Nissen
  • 1,153
  • 2
  • 11
  • 29
7

The answer is in the error message itself:

Error Locating Server/Instance Specified

Basically, you've got the wrong server\instance in the connection string, i.e. this bit:

Data Source=MSSQL2008-1

is wrong and not pointing to the server, or the servername doesn't resolve to an IP address. Two other posibilities: (1) the SQL Browser Service on the box running SQL Server isn't running or (2) Windows Firewall (or some other firewall) on the SQL box is denying incoming connections.

Ultimately, if the servername is correct, then it turns into a networking problem, and you need to find out why the client cannot connect to the server (and this will be more likely to be a basic networking problem than a SQL Server configuration problem).

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • Thanks for the answer but as I said I connect and retrieve data using that connectionstring, even the user validation occurs the only thing I doubt is the role provider class, so I did some test but i did not resolve the error. – ivi.hamiti Dec 07 '12 at 01:45
  • Sorry for not replying i had somewhere in the code hardwriten an other connection string. thanks anyway – ivi.hamiti Jan 26 '13 at 13:48
  • Also, if you are doing this from the command line, sqlcmd, you might be missing -d yourdatabase – Orn Kristjansson Nov 19 '15 at 18:11
5

If you are using Developer Edition of SQL Server change ".\SQLEXPRESS" to your computer name in connection string.

Before:

Data Source=.\SQLEXPRESS;Initial Catalog=SchoolDB-EF6CodeFirst;Integrated Security=True;MultipleActiveResultSets=True

After:

Data Source=DESKTOP-DKV41G2;Initial Catalog=SchoolDB-EF6CodeFirst;Integrated Security=True;MultipleActiveResultSets=True

emert117
  • 1,268
  • 2
  • 20
  • 38
3

In my instance, the name of the connectionString in the web.config file was spelled wrong. This is the name of the database context Entity Framework uses. I guess this is the error you get when EF can't match up the connectionString name with the context.

RMuesi
  • 2,852
  • 1
  • 24
  • 27
  • Similar to this, I had "Server=.\\...", where I needed to remove the ".\" because I had already specified name. – Cryptc Nov 18 '20 at 14:40
3

Change your .\SQLEXPRESS,and add your SQL express name only and it works for me

 <add name="BlogDbContext" connectionString="data source=your name here; initial catalog=CodeFirstDemo; integrated security=True" providerName="System.Data.SqlClient"/>
  • you saved my day..i was finding a solution for more than 2 hours....God Bless Your Mind..... – Qwerty Jan 22 '20 at 08:30
2

I had the same error and I started the SQL Server Express service and it worked. Hope this helps.

Hưng Ngô
  • 21
  • 1
1

We got the same issue recently. We found out that the code is looking for the connection string named "LocalSqlServer" in the machine.confg file. We added this line and it is working fine.

1

i had this issue, and fixed it. the problem seemed to be this:

wrong:

<add key="aaa" value="server=[abc\SQL2K8];database=bbb;uid=ccc;password=ddd;" />

right

<add key="aaa" value="server=abc\SQL2K8;database=bbb;uid=ccc;password=ddd;" />
Ash
  • 1,269
  • 3
  • 25
  • 49
1

The issue is caused by the DNS failing to resolve the hostname. Try using the IP address instead of the "computer name".

  • I just used an IP address but can't connect. The IP can ping but connect to SQL server from remote or even the local machine when changing ".\[instance name]" whit "[ip]\[instance name]" is impossible. – QMaster May 15 '19 at 13:44
1

In my case I searched for services. Then found a service called "SQL Server(SQLEXPRESS). Then right click on that. Select properties. Then click start. That's all. Solved the issue.

Exception handler
  • 172
  • 1
  • 3
  • 15
1

its not a big issue just change your server name see where to server name change:

enter image description here

how to find server name

  1. open run
  2. write 'cmd'
  3. write in console window "Hostname".

your server name will show you

thank you.

NajiMakhoul
  • 1,623
  • 2
  • 16
  • 30
0

This problem took me about one day, on one of mine ASP.NET MVC project fortunately i had the problem on my machine and not in production environment, so comparing web.config i see and removing that the error disappeared... a true challenge connect the SQL Server error 26 to this problem

David
  • 73
  • 5
  • 14
0

Same issue. In my case I solved setting the project as the "StartUp Project".

Antonio Santise
  • 302
  • 3
  • 5
0

Had the same issue then I used the connection string without ./ (like DESKTOP-E53DUML instead of this ./DESKTOP-E53DUML)

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25
0

It turned out there were 2 versions of SQL Server installed on the machine.
Removing the old one solved the issues on my side.

More details:
During the startup the services of both services were trying to run.
And the one which was loaded first was working properly and was blocking the second to start.
P.S. Stopping the services of the first server and manually starting the second wasn't working as well.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
0

Open your SQL Server Configuration Manager and under SQL Server Services enable the SQL Server state if it is stopped by right clicking and click on start.

Robera Negussie
  • 83
  • 1
  • 2
  • 11