title: Connecting to SQL Server database using C#
body: <p>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 <code>.aspx</code> page of the website but I am getting an error</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>I have gone through various websites and tried to connect via <code>web.config</code> as well  as c# but its does not seem to be possible.</p>
  
  <p>What I have tried so far</p>
  
  <p><code>web.config</code> file:</p>
  
  <pre><code>&lt;connectionStrings&gt;
    &lt;add name="CnStr" 
         connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;" 
         providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><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();
  </code></pre>
  
  <p>The other way I have tried is:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>I have made  database using following steps</p>
  
  <ul>
  <li>right click on project </li>
  <li>selecting SQL Server database to <code>App_Data</code> folder with name <code>Database.mdf</code></li>
  </ul>
  
  <p>Also If I try using add connections from data connections in database explorer, it's not accessing the <code>database.mdf</code> file and load only the templates e.g master, temp etc and not the folder in my <code>App_Data</code> folder and giving same error.</p>
  
  <p>I have gone through many questions in stack overflow and tried using them as well</p>