?
Solved

Connectionstring value is null in asp.net with c#

Posted on 2008-10-17
2
Medium Priority
?
1,372 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 1500 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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