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>
4

2 回答 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

于 2013-04-29T04:53:41.347 回答
1
namespace InspectionServices.Services
{
    public class ConfigManager
    {
        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["Inspection"].ConnectionString;
        }
    }
}
于 2013-04-29T05:54:15.303 回答