N/APosted on - 08/10/2011
My website won’t connect to the database. I am using SQL Server 2008 as database and I want to connect to it using webconfig.
I use a key inside AppSettings and put server name, database name, user id and password to the value of the key however it does not work.
I can assure you that all the values for the items above are correct so I don’t know why I am still getting this kind of error.
Please see image below for error details.
<addkey="ConnectionString"value="server=Myserver;database=Mydatabase;Trusted_Connection = true;"/>
An unexpected error has occurred.
Please see the details below:
Keyword not supported: 'userid'.
C# web Connection to database through web config error
WAY-1: app.config file:
Don't write that code in web config file. Instead you can use Application Configuration File.
Follow steps below
1. Add an "application configuration file" to your project solution.
(Right Click mouse on Solution Explorer > Select Add an item > you will get a window like the picture attached below.
Select Application Configuration File> Click Add )
2. Copy paste these lines of newly created Application Configuration File.
<?xml version="1.0" encoding="utf-8" ?>
<add key ="connectionSting" value ="server=Myserver; database=Mydatabase; integrated security=SSPI"></add>
3. Replace server name and database name with your own server and database
4. Write down SqlConnection(connectionString) parameter as
string connectionString = ConfigurationSettings.AppSettings["connectionSting"];
WAY-2: web.config file:
You can have separate web.config files for each directory in your web application, in addition to the one in the root. For each web page, by default, the system will look for a web.config in the same folder as the page and if not found, then looks in the parent folder. The web.config file with the connection string will have the following content.
< ?xml version="1.0"?>
< add key="DatabasePath"