Click here to Skip to main content
13,251,398 members (55,270 online)
Rate this:
Please Sign up or sign in to vote.

    <add name="connString" 
     connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\databasename.mdf;integrated security=True;connect timeout=30;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

Class.cs file

string conString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;

Now why am I Geting this error:"Object reference not set to an instance of an object". in my ConfigurationManager line?
Posted 18-Aug-12 12:21pm
Kenneth Haugland 18-Aug-12 18:20pm
Forgot a new block in your code?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This exception will obviously come when you'll try to point an object which is null.
Try this:
    <add name="ConnStringDb1" connectionstring="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providername="System.Data.SqlClient" />

To read the connection string into your code, use the ConfigurationSettings class.
if(ConfigurationManager.ConnectionStrings["myConnectionString"] != null){
    string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Abhinav S 18-Aug-12 23:59pm
_Amy 19-Aug-12 0:04am
Thank you Abhinav. :)
ridoy 19-Aug-12 3:04am
_Amy 19-Aug-12 3:05am
Thank you Ridoy. :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

string conString = ConfigurationManager.ConnectionStrings["connString"].ToString(); 

is sufficient. Plz try...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100