Solved

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

Posted on 2008-10-07
3
207 Views
Last Modified: 2012-05-05
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
Comment
Question by:tanu80
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22660089
0
 
LVL 25

Accepted Solution

by:
apeter earned 250 total points
ID: 22660652
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
 

Author Closing Comment

by:tanu80
ID: 31503845
i fix the issues after getting help from u
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
Gridview selected row 9 46
MediaHelp 4 23
Modal Popup Extender control 1 20
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question