Connection String

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 24-Jan-2005 09:50:44   

I am building up a business logic layer (using Adapter) that I want to use in a system that has both windows and web based elements.

In my business logic layer I use the command

DataAccessAdapter LLBLAdapter = new DataAccessAdapter(this.ConnectionString,false,
CatalogNameUsage.ForceName,new ConfigSetting().GetConfigSetting("DBCatalog"));

to create a connection to the database

which works fine for the Windows application because I have an XML file called Configuration.cfg that I hold in the root folder of the application. This file holds the information (DBServer, DBCatalog, DBUid and DBPwd) that I need to connect to the database. Therefore if the database moves I just edit this file in notepad. Obviously the DBUid and DBPwd fields are encrypted in this file.

ConfigSetting().GetConfigSetting("DBCatalog") is just a small class.method that I have built that opens the XML file and returns the value of the key passed, in this case the value of the key "DBCatalog".

public class ConfigSetting
    {
        public ConfigSetting()
        {
            // Constructor code
        }

        public string GetConfigSetting(string strKey)  
        {
            DataSet myDataSet  = new DataSet();
            string myReturnValue = "";
            // Attempt to locate the configuration.cfg file
            try
            {
                myDataSet.ReadXml(System.Windows.Forms.Application.StartupPath + "\\Configuration.cfg");

                foreach (DataRow row in  myDataSet.Tables[0].Rows)
                {
                    if (row["Key"].ToString() == strKey)
                    {
                        myReturnValue = row["Value"].ToString();
                    }
                }

                return myReturnValue;
            }
                
            catch (Exception ex)
            {
                throw new Exception("There is a problem reading the file " + 
                    System.Windows.Forms.Application.StartupPath + "\\Configuration.cfg. " + ex.Message);               }
            }
                
    }   

this.ConnectionString uses the ConfigSetting Class to retrieve and then return the full connection string to the SQL Server database.

My question is that although this approach works perfectly for a windows app, how can I make it work for a web app too - can I simply put the Configuration.cfg file in the root of the web site and use it as it is - I guess the answer to this is NO because the command

System.Windows.Forms.Application.StartupPath

cannot be used to locate the root of a web application.

So how might I inform my ConfigSetting class that the request is comming from a web application and therefore it should return the values from the Web.Config file instead? Is there a way to do this so that I dont have to change the comand

DataAccessAdapter LLBLAdapter = new DataAccessAdapter(this.ConnectionString,false,
CatalogNameUsage.ForceName,new ConfigSetting().GetConfigSetting("DBCatalog"));

in my business logic layer.

OR, am I completely missing a trick - how do other people achieve the same goal?

Thanks in advance

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 24-Jan-2005 10:40:16   

You could try to add the information to hte web.config file. There is a way to include config information into a .config file if I'm not mistaken, I don't know the syntaxis from my bare head but google should give you that. Which should solve the problem for you I think.

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 24-Jan-2005 11:23:24   

Thanks

You are right, If i inlcude the information and an app.config file for windows apps and web.config file for web then my ConfigSetting class just needs to be

public class ConfigSetting
    {
        public ConfigSetting()
        {
            // Constructor code
        }

        public string GetConfigSetting(string strKey)  
        {
            AppSettingsReader myAppSettings = new AppSettingsReader();
            try
            {
                return (string) myAppSettings.GetValue(strKey, typeof(string)); 
            }
                
            catch (Exception ex)
            {
                throw new Exception("There is a problem reading the file config file - " + ex.Message);             }
            }
                
    }   

and it will work for both.....

Why on earth did I try to build my own config system - what a muppet!