Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-07
3
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22660089
0
 
LVL 25

Accepted Solution

by:
apeter earned 750 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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