Sql-server – Can’t connect to SQL Server database in asp.net

asp.netsql server

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)

Best Answer

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