Click here to Skip to main content
12,623,511 members (27,445 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL-Server C#4.0 EF4.0
App.Config

<configuration>
  <connectionStrings>
    <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" />
  </connectionStrings>

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
Comments
Kenneth Haugland 18-Aug-12 18:20pm
   
Forgot a new block in your code?
Rate this: bad
 
good
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:
<connectionstrings>
    <add name="ConnStringDb1" connectionstring="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providername="System.Data.SqlClient" />
</connectionstrings>
To read the connection string into your code, use the ConfigurationSettings class.
if(ConfigurationManager.ConnectionStrings["myConnectionString"] != null){
    string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
}

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

Solution 1

string conString = ConfigurationManager.ConnectionStrings["connString"].ToString(); 
is sufficient. Plz try...
  Permalink  
v2

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 | Mobile
Web01 | 2.8.161128.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2016
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