Solved

Connectionstring value is null in asp.net with c#

Posted on 2008-10-17
2
1,339 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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