Solved

Connectionstring value is null in asp.net with c#

Posted on 2008-10-17
2
1,357 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
[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
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 Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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