Connection to SQL Server 2008 R2 isn't working
NickName:user3399326 Ask DateTime:2015-06-11T06:34:36

Connection to SQL Server 2008 R2 isn't working

I tried to test if my application is connected to the local database. I don't get any errors, so couldn't quite figure out why it's not working. I only get "no connection" output. I tried to debug it but get connection = null. I have SQL Server 2008 R2 (mixed authentication) and Visual Studio 2008 sp1. I tried connecting using both Windows and SQL Server authentication however neither worked.

This is my web.config file.

<connectionStrings>
    <add name="MyDbConn" 
         connectionString="Data Source=local;Initial Catalog=Sample;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

Default.aspx.cs

public partial class _Default: System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e) 
    {
    }
    protected void btnTestDb_Click(object sender, EventArgs e)
    {
        try {
            SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Sample; Integrated Security=SSPI");
            connection.Open();
            if (connection != null && connection.State == ConnectionState.Closed) {
                Response.Write("Connection OK!");
                connection.Close();
            } else {
                Response.Write("No Connection!");
            }
        } catch {
            Response.Write("No Connection!");
        }
    }
}

Copyright Notice:Content Author:「user3399326」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30768469/connection-to-sql-server-2008-r2-isnt-working

Answers
Rajesh Shetty 2015-06-11T04:00:36

//Try this\n SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[\"MyDbConn\"].ToString());\n\nprotected void btnTestDb_Click(object sender, EventArgs e)\n {\n try {\n con.Open();\n if (con.State == ConnectionState.Open)\n {\n Response.Write(\"Connection Open\");\n }\n else\n {\n Response.Write(\"Connection Closed\");\n }\n con.Close();\n } catch {\n Response.Write(\"No Connection!\");\n }\n}\n",


Steve 2015-06-10T22:44:45

You have some problems in your code that tries to open the connection. \n\nWhen you try to call connection.Open, the result is an exception if you have problems or simply a connection with its ConnectionState=Open.\nYour code instead gives the \"Connection OK\" message if the ConnectionState is closed, but of course, having just called Open, and if you don't have problems, then the ConnectionState is open.\n\nYou could try this code....\n\nprotected void btnTestDb_Click(object sender, EventArgs e)\n{\n string result = TestConnection();\n Response.Write(result);\n}\nprivate string TestConnection()\n{\n try\n {\n using(SqlConnection connection = new SqlConnection(\"....\"))\n {\n connection.Open();\n return \"Connection opened correctly\";\n }\n }\n catch(Exception ex)\n {\n return \"Error opening the connection:\" + ex.Message;\n }\n}\n",


More about “Connection to SQL Server 2008 R2 isn't working” related questions

Connection string of SQL Server 2008 R2 silent install

I have a Windows application that works with a local database (SQL Server 2008 R2). I'm trying to create an install for my application. I want the installer check if SQL Server 2008 R2 is availabl...

Show Detail

Connection to SQL 2008 R2

i have a windows server 2008 set up as a database server, i used to have MS SQL 2005 installed on it, due to some client changes i installed SQL 2008 R2 along with the 2005, the R2 is with a named

Show Detail

DB Connection works with SQL Server 2008 R2, but not with SQL Server 2014

I have to update a very old site using classic ASP and SQLOLEDB but the connection to SQL Server doesn't work using SQL Server 2014. It works for my client using SQL Server 2008 R2, but it doesn't...

Show Detail

PHP connection to MS SQL Server 2008 R2

I am implementing a PHP application that needs to be able to connect to both MS SQL Server 2012 and 2008 R2. Connection to 2012 works OK with this configuration Windows 7 64-bit IIS 7.5 PHP 5.5.1...

Show Detail

SQL Server 2008 R2 set up server issue

Firstly apologies if this appears a really dumb question but after installing SQL Server 2008 R2 I can't actually use it and keep getting the below message: A network-related or instance-specific

Show Detail

connecting sql server 2008 r2 to netbeans

i want to create an java application which has to create a database and later access it. My problem is that i am not able to connect sql server 2008 r2 database driver on to the netbeans.i have sql

Show Detail

Is there a LookupSet equivalent in SQL Server 2008 ... NOT r2?

Just spent the day figuring out how to properly use the LookupSet method only to find out it isn't supported on my production database which is NOT running SQL Server 2008 R2 (it is just running ol...

Show Detail

SQL Server 2008 R2 connection string

HI people. I had SQL Server 2005 on Windows XP 32bit and just used this connection string Server=.\SQLEXPRESS;database=GroupALD; Integrated Security=True" Now I have Windows 7 64bit and SQL Ser...

Show Detail

SQL Server 2008 R2 on Server 2012 R2 web site connection issue

I have been using this connection string with no problem on a number of databases and MS operating systems. Windows Server 2003, 2008 R2 and 2012 R2 Eval, all Express versions. I am now trying to ...

Show Detail

SQL Server 2008 R2 Periodically Does Not Accept Connections

I have a problem about connection pooling on Sql Server 2008 R2 environment (OS is Windows Server 2008 R2 X64). I am periodically seeing this in Sql Server error logs: Event ID: 18056

Show Detail