Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


i am not able to connect to database using connection string in web.config please help me , i am getting this type of error page, please help me how to write connection string in web.config using userid and password of my sql server


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)


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: 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)



Thanks
<big>sudheer.N</big>
Posted
Comments
sriman.ch 22-Dec-11 4:43am    
It amy be due to incorrect credentials(username and password) and incorrect datasource...check your connectionstring once or else provide your connectionstring....
sudheer.learner 22-Dec-11 5:03am    
<add key="TFAConnection" value="Data Source=192.168.2.13;Initial Catalog=TFA_Data;User Id=sudheerjksn;Password=sudheer@123;">
sudheer.learner 22-Dec-11 5:05am    
My Connection String is like that
<add key="TFAConnection" value="Data Source=192.168.2.13;Initial Catalog=TFA_Data;User Id=sudheerjksn;Password=sudheer@123;">

Basically, when you failed to connect to your SQL Server, the issue could be:
1) Network issue,
2) SQL Server configuration issue.
3) Firewall issue,
4) Client driver issue,
5) Application configuration issue.
6) Authentication and logon issue.
Check these
Steps to troubleshoot SQL connectivity issues[^]
SQL Server 2005 Connectivity Issue Troubleshoot - Part I[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part II[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part III[^]

BTW check this bunch also for future errors
 
Share this answer
 
Comments
AmitGajjar 22-Dec-11 7:44am    
5 for you :)
Hi Sudheer,

hope this will help u

XML
<connectionStrings>
        <add name="Default" connectionString="Data Source=Ur server IP;User Id=sa;password=sa;Initial Catalog=UR DB Name />   </connectionStrings>



Regards,
Sucharitha
 
Share this answer
 
v2
Comments
sudheer.learner 22-Dec-11 5:35am    
Hi Sucharitha,

thanks for your solution , but now also i am not able to connect it shows some error
could you please send me connection string and how to access that in c#.net
i use in web.config
<connectionstrings>
<add name="TFAConnection" connectionstring="Data Source=192.168.2.13;User Id=sudheerjksn;password=sudheer@123;Initial Catalog=TFA_Data ">



and access this in c#.net is
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TFAConnection"].ConnectionString);


it show error like error: 40 - Could not open a connection to SQL Server
please help me sucharitha
sudheer.learner 22-Dec-11 5:36am    
<connectionstrings>
<add name="TFAConnection" connectionstring="Data Source=192.168.2.13;User Id=sudheerjksn;password=sudheer@123;Initial Catalog=TFA_Data ">

Hi Sudheer,

Some check list

1. Check that your SQL server is exposed to tcp/ip protocol using SQL Server Configuration Manager.
2. Check the format of your connection string

<add connectionstring="Server=<Server Name>;User ID=<User Name>;Password=<Password>;Database=<DatabaseName>" name="<ConnectionStringName>" />


3. Check that your SQL Server is using mixed mode for the authentication.

Thanks
Vinod
 
Share this answer
 
Comments
sudheer.learner 22-Dec-11 6:01am    
Now also i am not able to connect Vinod , could you please connect to my System
throw teamviewer
<AppSetting>
<add key="SqlConnectionString" value="Data Source=ServerName;DataBase=DatabaseNAme;UID=sa;PWD=pass;Min Pool Size=10; Max Pool Size=100"/>
</AppSetting>
check your name which you are enter.
 
Share this answer
 
XML
<AppSetting>
<add key=conn; value="Data Source=ServerName;DataBase=DatabaseName;User ID=sa;Password=pass"
oto->solution explorer->click->myproject->setting.setting->open bialog like name="conn",type="connection string;scope=application;value=""value="Data Source=ServerName;DataBase=DatabaseName;User ID=sa;Password=pass";
 
Share this answer
 
Try this

Solution 1 :

If your SQL server is SQL Server Authentication then just use following Connectionstring



<AppSetting>
<add key=Connectionstring; value="Data Source=ServerName;DataBase=DatabaseName;UserId=sa;Password=sa">
</AppSetting>

And access this Connectionstring as

using System.Configuration;
string str = ConfigurationManager.AppSettings["Connectionstring"];

Solution 2 :

If your SQL server is Windows Authentication then just use following Connectionstring


<AppSetting>
<add key=Connectionstring; value="Data Source=ServerName;DataBase=DatabaseName;Integrated Security=false">
</AppSetting>


using System.Configuration;
string str = ConfigurationManager.AppSettings["Connectionstring"];


I think this will solve the problem...
 
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