0

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

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

Make your "str" variable as static.

static string str = ConfigurationManager.ConnectionStrings["Bank"].ConnectionString;
SqlConnection con = new SqlConnection(str);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.