Using Visual Studio 2012 which was recently installed but now I can't connect to our SQL Server database.
These are the steps I'm following
- create
App1.config
type this in
App1.config
:<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name ="xxx" connectionString="USER ID=xx;PASSWORD=xx;PERSIST SECURITY INFO=True;Data Source=xx;Initial Catalog=xx" /> </connectionStrings> </configuration>
Add a reference to the project to
System.Configuration
Create access to namespaces via:
using System.Data.SqlClient; using System.Data; using System.Configuration;
implement the following:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Data; using System.Configuration; namespace ConsoleApplication10 { class Program { static void Main(string[] args) { SqlConnection conn = null; conn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString); } } }
I've created a new console app and added the above and I still get an error
NullReferenceException was unhandled
Object reference not set to an instance of an object.
...
EDIT
Via the immediate window I determined that the following is null:
ConfigurationManager.ConnectionStrings["xxx"].ConnectionString
If I hard-code the connection string into the constructor for SqlConnection
then it connects ok
What am I missing - something really obvious!! Or is this in connection with my new VS ?