0

I am using VS 2012 Express for web, I have created a website project and I am trying to connect integrated SQL Server with on the .aspx page of the website but I am getting an error

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)

I have gone through various websites and tried to connect via web.config as well as c# but its does not seem to be possible.

What I have tried so far

web.config file:

<connectionStrings>
  <add name="CnStr" 
       connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;" 
       providerName="System.Data.SqlClient"/>
</connectionStrings>

C# code:

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CnStr"].ConnectionString;

SqlConnection conn = new SqlConnection("connectionString");

SqlCommand cmd = new SqlCommand("select * from user_login ",conn);
conn.Open();

SqlDataReader rdr = cmd.ExecuteReader();

GridView1.DataSource = rdr;
GridView1.DataBind();

conn.Close();

The other way I have tried is:

SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;Database=Visual Studio 2012\\App_Data\\Database.mdf;Integrated Security=True");

SqlCommand cmd = new SqlCommand("select * from user_login ",conn);
conn.Open();

SqlDataReader rdr = cmd.ExecuteReader();

GridView1.DataSource = rdr;
GridView1.DataBind();

conn.Close();

I have made database using following steps

  • right click on project
  • selecting SQL Server database to App_Data folder with name Database.mdf

Also If I try using add connections from data connections in database explorer, it's not accessing the database.mdf file and load only the templates e.g master, temp etc and not the folder in my App_Data folder and giving same error.

I have gone through many questions in stack overflow and tried using them as well

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sm.ali
  • 65
  • 1
  • 12
  • possible duplicate of [Cannot Connect to Server - A network-related or instance-specific error](http://stackoverflow.com/questions/18060667/cannot-connect-to-server-a-network-related-or-instance-specific-error) – Raging Bull Jul 28 '15 at 10:06
  • I have tried this before,SQL Server they have used is external and not the built in one in visual studio,I am using the built in one and I cant find any configuration settings here – sm.ali Jul 28 '15 at 10:20
  • Seems something wrong with connection string. https://www.connectionstrings.com/sqlconnection/attach-a-database-file-located-in-the-data-directory-on-connect-to-a-local-sql-server-express-instance/ – Zen Jul 28 '15 at 10:24

2 Answers2

0

This is not code error, but a SQL configuration error. Follow steps in this excellent article for troubleshooting. http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
  • I am using the built in sql server or integrated one in visual studio,I have seen the link you answered Do you think its the same procedure with builtin sql server? cz i cant find any configuration tools here.I am using windows 8 – sm.ali Jul 28 '15 at 10:06
  • Are you connecting to this local server from elsewhere/different machine? – DhruvJoshi Jul 28 '15 at 10:11
  • No its on the same machine – sm.ali Jul 28 '15 at 10:13
  • Please type in services.msc in the windows run dialog(window+r key) and see if the sql browser service is running? If not please start it. Also change your datasource part from Data Source=.\SQLEXPRESS to Data Source=machine-name\SQLEXPRESS – DhruvJoshi Jul 28 '15 at 10:19
  • If this does not resolve it, I'd say check your .NET framework. It should be 4.5, if not, please update it. – DhruvJoshi Jul 28 '15 at 10:27
  • SQL server is running! – sm.ali Jul 28 '15 at 10:28
  • can you tell me a way to make my .mdf file visible in Add Connections>Select or Enter a database name – sm.ali Jul 28 '15 at 10:33
0

try this code instead!!!

    string str = "Data Source=(LocalDB)\\v11.0;Database=Visual Studio 2012\\App_Data\\Database.mdf;Integrated Security=True";

    using(SqlConnection conn = new SqlConnection(str));
    {
       conn.Open();
       using(SqlCommand cmd = new SqlCommand("select * from user_login ",conn);
       { 

           SqlDataReader rdr = cmd.ExecuteReader();

           GridView1.DataSource = rdr;
           GridView1.DataBind();
        }
     conn.Close();
  }