1
votes

I am using the configurationmanager.appsetting and I'm getting an error in the cn.Open line.

The web form code is as follows:

public partial class admin_CheckDocuments : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["con"]);
    string sql;
    string str;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        str = Request.QueryString["taxid"];

        if (!IsPostBack)
        {
            filldata();
        }
    }

web.config is:

<configuration>
    <connectionStrings/>
    <appSettings>
       <add key="con" 
            value="Data Source=EGGG\SKDB;Initial Catalog=tax;Integrated Security=true"/>
    </appSettings>

The error is

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)

2
the error is{"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)"} - sisodia1991
@user1792379 Do you have a local instance of Sql Server running or one running at the location designated (EGGG\SKDB)? - Robert

2 Answers

5
votes

The reasons for throwing this issue could be wrong server name, disabled remote connection and firewall blocking.

Could you please follow the steps below to solve this issue?

·Check the server on which SQL Server is running can be accessible. You can use ping command to test that. For instance, ping ** or ping . The ping command may be block by the firewall, make sure ICMP is enabled in the firewall. More info**

·Choose appropriate protocol

·Configure Windows firewall accordingly based on what protocol you have chosen to use. For detailed information about how to configure Windows Firewall to allow SQL Server, please check More info

·Enable SQL Server Browser Services

You need to enable SQL Server Browser Services if the following are both true:

1.SQL Server is not listening on default 1433 port or not use default pipe name \.\pipe\sql\query;

2.The corresponding TCP port or pipe name is not specified in the connection string (such as Srv1\SQL2008, 1500).

If you have enabled SQL Server Browser Services, you still need to open UDP 1434 port which is used by Browser Services in the Windows firewall.

Let me know if that gets resolved

2
votes

I noticed this in your connection string:

Integrated Security=true

This means that your code will try connect using the credentials for the server account running the asp.net process. That's often a local account like Network Service, Local Service, or IIS_IUSRS. You can change this, but by default you're not likely to have rights to even log in to the database.