Raja Khoury

Raja Khoury

  • NA
  • 10
  • 6.1k

Error Connecting to SQL Server. Why?

Jun 21 2012 10:29 AM
Hi there,

I have been facing an error connecting to SQL server, I setup a firewall exception for both SQL server and Visual studio. 

I first created the app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myConnection" connectionString="Data Source=RAJA;Initial Catalog=strong; Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

And this is the code in the windows app form:

    private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection myConnection = new SqlConnection("Data Source=RAJA;Initial Catalog=strong;Integrated Security=True; Connection Timeout=10;");
                SqlCommand cmd = new SqlCommand();
                Object returnValue = null;
                cmd.CommandText = "SELECT COUNT(*) FROM Dictionnary";
                cmd.Connection = myConnection;
                myConnection.Open();
                returnValue = cmd.ExecuteScalar();
                myConnection.Close();
                textBox2.Text = returnValue.ToString();
                textBox2.Text = "hello";
            }
            catch (Exception Ex)
            {
                textBox2.Text = Ex.ToString();

            }


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




at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at strong.Form1.button1_Click(Object sender, EventArgs e)




Why I am not able to connect to SQL server? the server is running I checked it the configuration manager and I added the firewall exceptions. Did I forget anything? Thanks

Answers (7)