Solved

Connectionstring value is null in asp.net with c#

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

16 Experts available now in Live!

Get 1:1 Help Now