1

i am trying to get connection string from method resulting error object reference not set to the instance object my method is,

this method return connection,

namespace InspectionServices.Services
{
    public class ConfigManager
    {
        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["***"].ConnectionString;
        }
    }
}

and here i am getting connection by calling method mentioned above,

 string connectionString = InspectionServices.Services.ConfigManager.GetConnectionString();

hopes for your suggestion

Thanks in Advance

EDITED:

Appconfig,

 <connectionStrings> 
    <add name="Inspection" connectionString="Data Source=***;Database=***;Integrated Security=SSPI; Persist Security Info=false; Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
1
  • 2
    Please also show us your web.config file! Do you have a <connectionStrings> section in there? Does the connection string you want to load even exist in that section?? Commented Apr 29, 2013 at 4:52

2 Answers 2

1

You mush have web.config entries like this

<connectionStrings>
     <add name="name" connectionString="***" />
</connectionStrings>

Only then you can use it in your class.

Some useful links
C# Configuration Manager . ConnectionStrings
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

Sign up to request clarification or add additional context in comments.

5 Comments

i am getting error on object refrence not set to instance of object on the methos from where i am returning connection string
at which line is it in return ConfigurationManager.ConnectionStrings["***"].ConnectionString; line?
are you putting *** or Inspection in the above line?
@ one thing i noticed that as i gave it index in place of name "inspection " it successfully get connection string then what would be the reason ???
It is case sensitive so be careful. You might be using inspection in place of Inspection
1
namespace InspectionServices.Services
{
    public class ConfigManager
    {
        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["Inspection"].ConnectionString;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.