

- Asp.net sql server connection string how to#
- Asp.net sql server connection string code#
- Asp.net sql server connection string password#
If you click Test Connection again and receive another error message, you may wish to read my piece on How to fix common SQL Server Management Studio 'Connect to Server' errors. Looking at it myself, I still think, there must be an easier way.To validate your connection, use the Test Connection feature, and click OK if you receive a "test connection succeeded" notification.īut.you may get an error message, which means you'll probably need to double-check the server name, log on credentials, or authentication entries.

I have looked all over for something like this but I eventually figured it out after many weeks of hard work.

I also would appreicate feedback on how to improve these steps if possible. So I am sure there must be a better way to do this code. Hense the reason you test the constring first in its origional format so you know if it is a problem with the connection string or if it is a problem with the code.
Asp.net sql server connection string code#
You will replace it with this line of code: SqlConnection con = new SqlConnection(conString.ConnectionString)Īfter doing these 5 steps your code should work as it did before. This gets the connection string from the web.config file with the name conString (from the constant above) ConnectionStringSettings conString = įinally, where you origionally would have had something similar to this line of code: SqlConnection con = new SqlConnection(conString) Next add the following line of code to the C# code behind. const string CONSTRINGNAME = "conString" This sets up a string constant to which you can refer in many places throughout your code should you need a conString in different methods. Now add the following line of code to the C# code behind. static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/MyProject") This is usually the location of the web.config file (in this case my project is called MyProject. This points to the root folder of your project. Now add the following line of code to the C# code behind, prefrably just under the class definition (i.e. Now go into the web config, and magically, you will see nice clean working connection string there with all the details you need.
Asp.net sql server connection string password#
So setup SSMS on a computer and ensure you can access the server with the username and password to that database server. Next (if needed), if you are trying to access the database on a separate server, ensure you can do likewise in SSMS. Start obviously by logging into the database server using SSMS (Sql Server Management Studio or it's equivalent in other databases) locally to ensure you have access using whatever details you intend to use. So I will write the steps below.īefore you setup your connection string in code, ensure you actually can access your database. I found this very difficult to get an answer to but eventually figured it out.
