C# web Connection to database through web config error

Asked By 0 points N/A Posted on -
qa-featured

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.  

 

  <appSettings>  

<addkey="ConnectionString"value="server=Myserver;database=Mydatabase;Trusted_Connection = true;"/>

</appSettings>

An unexpected error has occurred.

Please see the details below:

Keyword not supported: 'userid'.

SHARE
Answered By 0 points N/A #113804

C# web Connection to database through web config error

qa-featured

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" ?>

<configuration>
  <appSettings>
    <add key ="connectionSting" value ="server=Myserver; database=Mydatabase; integrated security=SSPI"></add>
  </appSettings>
 
</configuration>

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"?>

      < configuration> 

                  < appSettings>

                        < add key="DatabasePath"

                            value="server=localhost;

                             database=FinAccounting;

                             Integrated Security=true;"/>

                   < /appSettings>

       < /configuration>

 

Related Questions