Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 at 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 at 18-Aug-12 23:59pm
   
5!
_Amy at 19-Aug-12 0:04am
   
Thank you Abhinav. :)
ridoy at 19-Aug-12 3:04am
   
+5
_Amy at 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
0 Maciej Los 347
1 DamithSL 305
2 ridoy 278
3 OriginalGriff 224
4 George Jonsson 140
0 OriginalGriff 7,968
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,524
3 Maciej Los 5,438
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2014
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