Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Not able to read the value of Connection String: from Config file in asp.net with c#

Hi I am using  a class library for retuning the connection string in configfile.i have created a config class and configconstanr class.In configconstant class i have defined the connection string name.And in the config class i m returing the connection string.The beleow code i am using

namespace MyLifeSpaceCommonExt.Common
{
    public class Config
    {
        public static string MyLifeSpaceConnectionStringFromDLLConfig
        {
            get { return GetConnectionStringFromDLLConfig(Constants.ConfigConstants.MyLifespaceConnectionString); }
        }
        public static string GetConnectionStringFromDLLConfig(string name)
        {
            ConnectionStringSettings conn ;

            string value = null;
            try
            {
                conn = GetConfig().ConnectionStrings.ConnectionStrings[name];
                value = conn.ConnectionString;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Not able to read the value of Connection String: '{0}' from Config file", name), ex);
            }
            return value;
        }
        public static Configuration GetConfig()
        {
            string dllName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".dll";
            string path = System.IO.Path.Combine(MyLifeSpaceCommonLibExtFromWebConfig, dllName);
            Configuration config = ConfigurationManager.OpenExeConfiguration(path);
            AssemblyInfo a = new AssemblyInfo(dllName);
            return config;
        }
        public static string MyLifeSpaceCommonLibExtFromWebConfig
        {
            get { return GetAppSettingsFromWebConfig(Constants.ConfigConstants.MyLifeSpaceCommonLibExtPath); }
        }

        public static string GetAppSettingsFromWebConfig(string name)
        {
            string value = "";
            try
            {
                value = WebConfigurationManager.AppSettings[name];
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Not able to read the value of '{0}' from Config file", name), ex);
            }
            return value;
        }

       
    }
}


in GetConnectionStringFromDLLConfig method i m getting connectionstring value as null.

So looking for help,

Thanks
Tanayya
0
tanu80
Asked:
tanu80
1 Solution
 
Bob LearnedCommented:
0
 
apeterCommented:
Is your Config file is like below ? and you are trying to fetch "ConnStr1' according to below example ?

<configuration>
  <connectionStrings>
    <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
0
 
tanu80Author Commented:
i fix the issues after getting help from u
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now