Hello to all,
I am calling connection string from app.config
string str = ConfigurationManager.ConnectionStrings["Bank"].ConnectionString;
SqlConnection con = new SqlConnection(str);

my app.config file is
<configuration>

<connectionStrings>

<add name="Bank"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BankManagement(2)\BankManagement\bin\Debug\BankManagement.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />


</connectionStrings>


</configuration>

bt i am getting compilation error:

Error 1 A field initializer cannot reference the non-static field, method, or property

Recommended Answers

All 2 Replies

Read about Compiler Error CS0236. If str and con are both instance fields of a class, you can't use str to initialize con like that.

Make your "str" variable as static.

static string str = ConfigurationManager.ConnectionStrings["Bank"].ConnectionString;
SqlConnection con = new SqlConnection(str);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.