Correctly declaring ConfigurationManager when connecting to database

sheron picture sheron · Apr 15, 2011 · Viewed 25.6k times · Source

How do I solve the error message "ConfigurationManager not declared" when used to connect to database?

Here is the line of code which gives the error:

SQLConnStr= ConfigurationManager.ConnectionStrings("SQLConnStr").ConnectionString

Answer

Crimsonland picture Crimsonland · Apr 15, 2011

Have you tried to Add Reference

  `System.Configuration` 

on you References Folder.

Just simply right click your references folder on your project then click add reference.. and then look for System.Configuration then ok.

then add

Imports System.Configuration 

on your form

Change you app config to this:

<connectionStrings> 

<add name="SQLConnStr" connectionString="Data Source=HOTEL_MySQLSERVERR;Initial Catalog=GuestBooking;User ID=xyz;Password=xyz" providerName="System.Data.SqlClient" /> 

</connectionStrings>

//your app config name and connection string name must be the same

add name="SQLConnStr"

ConnectionStrings("SQLConnStr").

SQLConnStr= ConfigurationManager.ConnectionStrings("SQLConnStr").ConnectionString

and have a look at this:

Fix the Name ‘ConfigurationManager’ is Not Declared Error

Regards