Solved

Connectionstring value is null in asp.net with c#

Posted on 2008-10-17
2
1,328 Views
Last Modified: 2013-11-26
i have created an application by using class libray.One class libray i have created for  database connection and another class library for Database acess.I am calling the class library in my application by adding the two solution file of the class libraries.
i am using the code for connection string class libary as:
using System.Configuration;
using System.Web.Configuration;
using System.Security;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Linq;
using System.Web;
namespace Common1ext.Common
{
    public class Config
    {
        public static string ConnectionStringFromDLLConfig
        {
            get
            {
                try
                {
                    return GetConnectionStringFromDLLConfig(Constants.ConfigConstants.ConnectionStringName);
                }
                catch(Exception ex)
                {
                    return "";

                }
            }

        }
        public static string GetConnectionStringFromDLLConfig(string name)
        {
            ConnectionStringSettings conn;
            string value = null;
            // = default(ConnectionStringSettings); 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(AsbestosCommonLibExtFromWebConfig, dllName);
            Configuration config = ConfigurationManager.OpenExeConfiguration(path);
            AssemblyInfo a = new AssemblyInfo(dllName); return config;


        }
        public static string AsbestosCommonLibExtFromWebConfig
        {
            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;
        }

    }
}

for setting connectiostringname i have created another class
using System;
using System.Data;
using System.Configuration;
using System.Linq;


namespace Common1ext.Constants
{
    public class ConfigConstants
    {
        public const string ConnectionStringName = "MyLifeConnectionString";
        public const string MyLifeSpaceConnectionString = "MyLifeSpaceConnectionString";
        public const string MyLifeSpaceCommonLibExtPath = "MyLifeSpaceCommonLibExtPath";
    }
}
and in the app config file i have used like
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyLifeConnectionString" connectionString="Data Source=LENOVO-2DB4B5C7\SQLEXPRESS;Initial Catalog=tanaya;Integrated Security=True " providerName=" System.Data.SqlClient "/>
    <!--<add name=" LJVWMSCRAMConnectionString " connectionString=" Data Source=CHANDRABHAN\CHANDRABHAN;Initial Catalog=Asbestos;User ID=sa;Password=mindmill " providerName=" System.Data.SqlClient "/>-->
  </connectionStrings>
</configuration

Then i am calling class libray dll in my Main project web config file as
<appSettings>
            <add key="MyLifeSpaceCommonLibExtPath" value="c:\MyLifeSpace\Common1ext\Common1ext\bin\Debug"/>
            <!--<add name="MyLifeConnectionString" value="Data Source=LENOVO-2DB4B5C7\SQLEXPRESS;Initial Catalog=tanaya;Integrated Security=True " SqlClient "/>-->
      </appSettings>
      <connectionStrings>
            <!--<add name = "AsbestosConnectionString" connectionString = " Data Source=LENOVO-2DB4B5C7\SQLEXPRESS;Initial Catalog=tanaya;Integrated Security=True " providerName = " System.Data.SqlClient " />-->
            <!--<add name = " LJVWMSCRAMConnectionString " connectionString = " Data Source=CHANDRABHAN\CHANDRABHAN;Initial Catalog=Asbestos;User ID=sa;Password=mindmill " providerName = " System.Data.SqlClient " />-->
            <!--<add name="MyLifeConnectionString" connectionString="Data Source=LENOVO-2DB4B5C7\SQLEXPRESS;Initial Catalog=tanaya;Integrated Security=True " providerName=" System.Data.SqlClient "/>-->
      </connectionStrings>

every thing is working fine in local end but when i am publishing the main project application and uploading the published project in to the server i am getting error like

Server Error in '/Dev/mylifespace' Application.
--------------------------------------------------------------------------------

The value can not be null or an empty string.
Parameter name: connectionString
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The value can not be null or an empty string.
Parameter name: connectionString

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[ArgumentException: The value can not be null or an empty string.
Parameter name: connectionString]
   Microsoft.Practices.EnterpriseLibrary.Data.Database..ctor(String connectionString, DbProviderFactory dbProviderFactory) +171
   Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase..ctor(String connectionString) +43
   Libext1.DAL.MyPersonalDB.GetAllCountryData() in C:\Project\Libext1\Libext1\DAL\MyPersonalDB.cs:96
   MyLogin.Registration.BindCountry() in C:\MyLogin\MyLogin\Registration.aspx.cs:144
   MyLogin.Registration.Page_Load(Object sender, EventArgs e) in C:\MyLogin\MyLogin\Registration.aspx.cs:64
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

 
i am really confused about the situation, if every thing is working fine in local end then why it is making problem in uploading to the server, i am guessing the uploaded application couldnt find the configuration dll for setting the connection string value....any can short my problem or give me idea what can be the possible solution
0
Comment
Question by:tanu80
2 Comments
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 500 total points
ID: 22744347
when the application runs in server the dll will run in exe's context hence when you call OpenExeConfig method it will be opening the configuration file for the EXE and not the dll. So it wont be able to find connection string ni the exe's config file.

I would suggest you to use OpenMappedexe config method

details here
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration(VS.80).aspx
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22746835
use the ConfigurationManager.ConnectionStrings to get the connection string from the another projects app configuration
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now