bharathi
bharathi

Reputation: 6271

C# ConfigurationManager Problem

I have upgrade the project from framework3.5 to framework4.0.Right Now i am using Visual studio 2010.Here is my app.config file

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 <appSettings>
<add key="DBConnectionString" value="User ID=sa;Password=password123;Initial   Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D"/>
 </appSettings>
  </configuration>

Here when i am working with framework 3.5 I used the config file as

  using  System.Configuration;
namespace Voting_Editor_Tool_New
{
public partial class Voting_Editor_Tool : Form
{
    SqlConnection myConnection;

    string connectString = ConfigurationSettings.AppSettings["DBConnectionString"];
  public void getdata()
  {



   myConnection = new SqlConnection(connectString); 
     ....
   } 
 }
}

When I upgrade to framework 4.0 the line

   ConfigurationSettings.AppSettings["DBConnectionString"];

shows a warning message as

'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'.

I tried with ConfigurationManager.AppSettings["DBConnectionString"]; but it shows a error as

The name 'ConfigurationManager' does not exist in the current context

Can anyone just how to solve the problem.Thanks in advance.

Upvotes: 0

Views: 4568

Answers (3)

Felipe Clemente
Felipe Clemente

Reputation: 57

I had solved my problem changing the string "connectString" for the really string that is the address of your DB.

Exemple: this = myConnection = new SqlConnection(connectString); For this = myConnection = new SqlConnection("User ID=sa;Password=password123;Initial Catalog=DishTV_Voting;Persist Security Info=True;Data Source=ENMEDIA-50CB48D")

In my case works perfect

Upvotes: 0

Alex R.
Alex R.

Reputation: 4754

Make sure you are referencing System.Configuration 4.0. Try to remove the existing reference, and add the latest version of it again.

Then you can get the actual connection string, and create an SQL connection as such:

using (SqlConnection conn = new SqlConnection(
   ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString))
{
    conn.Open();
    // do stuff
}

Reference

Upvotes: 0

rerun
rerun

Reputation: 25495

You have to add a reference the System.configuration.

Upvotes: 4

Related Questions