2

I have defined my connection String in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>

    <add name="CString" 
         connectionString="Data Source=Bilal-PC;Initial Catalog=ATMSoftware;Integrated Security=False; User Id=sa; Password=123" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Now I want to get it into my C# class and I have tried all the methods. but I am getting an error on configuration manager.

Please help me.

Community
  • 1
  • 1
Snake
  • 337
  • 2
  • 7
  • 23

2 Answers2

4

Ensure that you have added a reference to System.Configuration in your project, and place a

using System.Configuration;

statement at the top of your source. The ConfigurationManager type should now be available.

string connectionString = ConfigurationManager.ConnectionStrings["CString"].ConnectionString;
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • By using System.Configuration; it is not available – Snake Jan 01 '12 at 14:20
  • The question is tagged [homework] - when that happens, we try to give answers that teach, not a complete solution. – Oded Jan 01 '12 at 14:20
  • I'll leave teaching as the reward for the inquistive mind. – devdigital Jan 01 '12 at 14:23
  • 1
    Bilal, have you added the System.Configuration assembly as a reference to your project? In Visual Studio, right click References, Add Reference, .NET, System.Configuration. – devdigital Jan 01 '12 at 14:25
1

See MSDN for more information

var conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"];
string strConnString = conString.ConnectionString;
Shai
  • 25,159
  • 9
  • 44
  • 67