Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
Everything seems to be perfect and i typed in the very same code as
illustrated in a youtube ADO.net tutorial ->
https://www.youtube.com/watch?v=aoFDyt8oG0k[^]
by Kudvenkat.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("data source=.;
database=sample ; integrated security=SSPI");
        SqlCommand cmd = new SqlCommand("Select *from student", con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        GridView1.DataSource = rdr;
        GridView1.DataBind();
        con.Close();
    }
}


But while running the same code in browser ,i encounter 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: Named
Pipes Provider, error: 40 - Could not open a connection to SQL
Server)" pointing to con.open(); line.

Regarding connection I have connected the paricular database in SERVER EXPLORER>
What is the Remedy for this ?
Posted
Updated 19-Feb-15 20:57pm
v3
Comments
Suvendu Shekhar Giri 20-Feb-15 2:55am    
Are you able to logged in to the SQL Server Management Studio with
Server Name="." and with windows authentication.

This is a very common problem and there are a lot of potential reasons like:
- firewall
- service down
- instance name not specified and so on.

Please go through http://www.sqlmusings.com/2009/03/11/resolving-a-network-related-or-instance-specific-error-occurred-while-establishing-a-connection-to-sql-server/[^]
 
Share this answer
 
SqlConnection con = new SqlConnection(@"data source=.;
database=sample ; integrated security=SSPI");

just prefixed @ before connectionstring and it solved!
 
Share this answer
 
The problem here is your application is not been able to connect to database.. due to wrong credentials or improper connection string... The error must be showing while opening the connection...
Please try this connection string...
C#
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;

you can also get appropriate connection string from here.[^]

If this solves your issue kindly up vote and mark this as solution.. Thanks..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900