Calling Connection string from App setings app.config file to C# code

LiquidForce

New Member
I'm trying to call a connection string from the app.config file to C# code. This is the app.config code:\[code\]<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings><add key="connectstring" value="http://stackoverflow.com/questions/10731482/Data Source=server111;Initial Catalog=database1; Integrated Security=False;Persist Security Info=False; User ID=username;Password=password;Encrypt=True; TrustServerCertificate=True; MultipleActiveResultSets=True"/> </appSettings></configuration>\[/code\]This is the C# code: \[code\] private SqlConnection connStudent; connStudent = new SqlConnection(ConfigurationManager.AppSettings["connectstring"].ToString()); connStudent.Open();\[/code\]The code should be right, but I'm getting a Null Reference Exception, and while debugging the program, connStudent is always null and does not get the connection string .The error is "Object reference not set to an instance of an object".
 
Back
Top