Re: Passing the connection string from a web form to a business lo
- From: "Juan T. Llibre" <nomailreplies@xxxxxxxxxxx>
- Date: Wed, 9 Nov 2005 16:18:42 -0400
re:
> I'm reading a value from App.config
Isn't System.Configuration.ConfigurationSettings.AppSettings
hard-wired to read from web.config if you're writing a web application?
Are you talking about a Windows Forms app.config ?
Windows applications in the .NET Framework use
the name app.config by default for their configuration file.
Web applications use web.config by default for the configuration file.
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Mike L" <Cadel@xxxxxxxxxxxxx> wrote in message
news:D69F0899-9328-4296-B41F-AC235339C50A@xxxxxxxxxxxxxxxx
> I'm reading a value from App.config
> My project is a C# 2005 win form application.
>
> Should I ignore the warring messages for "string sConnString =
> System.Configuration.ConfigurationSettings.AppSettings["dsn"];" ?
>
> "Juan T. Llibre" wrote:
>
>> Just import system.configuration and use :
>> string sConnString = System.Configuration.ConfigurationManager.AppSettings["dsn"];
>>
>> The thing is that the ConfigurationManager class isn't meant to be used
>> just to read a key and insert the key's string value into a connection.
>>
>> It's meant to be used to *modify* configuration files.
>>
>> If you just want to read a value in web.config, use :
>> System.Configuration.ConfigurationSettings.AppSettings["dsn"];
>>
>>
>>
>>
>> Juan T. Llibre, ASP.NET MVP
>> ASP.NET FAQ : http://asp.net.do/faq/
>> ASPNETFAQ.COM : http://www.aspnetfaq.com/
>> Foros de ASP.NET en Español : http://asp.net.do/foros/
>> ======================================
>> "Mike L" <Cadel@xxxxxxxxxxxxx> wrote in message
>> news:F1CF1091-F405-4E63-B72D-8F51DC776ABE@xxxxxxxxxxxxxxxx
>> > 'System' does not exist in the namespace 'System.Configuration'
>> >
>> > I added C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll
>> > I also added using System.Configuration;
>> >
>> > Here is the line of code it errors on.
>> > string sConnString =
>> > System.Configuration.System.Configuration.ConfigurationManager.AppSettings["dsn"];
>> >
>> >
>> > For now, I'm staying with ConfigurationSettings and live with the warnings.
>> > string sConnString =
>> > System.Configuration.ConfigurationSettings.AppSettings["dsn"];
>> >
>> >
>> > "Juan T. Llibre" wrote:
>> >
>> >> Hi, Greg.
>> >>
>> >> re:
>> >> > Can anyone explain?
>> >>
>> >> System.Configuration.ConfigurationSettings.AppSettings
>> >> is a class in the System.Configuration namespace in system.dll.
>> >>
>> >> System.Configuration.ConfigurationManager.AppSettings isn't.
>> >>
>> >> See :
>> >>
>> >> http://www.asp.net/QUICKSTART/util/classbrowser.aspx?namespace=System.Configuration
>> >>
>> >> Configuration.ConfigurationManager is a class which resides in
>> >> System.Configuration (in system.configuration.dll), not in system.dll
>> >> which is where Configuration.ConfigurationSettings.AppSettings resides.
>> >>
>> >> It's a bit confusing because the System.Configuration first names seem the same,
>> >> but -just like you do with people- you need a first name *and* a last name
>> >> to identify individuals.
>> >>
>> >> The System.Configuration class in System.dll is really System.System.Configuration
>> >> ( notice the *two* "system" in the name ) but we don't have to identify the base
>> >> namespace because it's automatically imported in all aspx pages.
>> >>
>> >> If you want to use the ConfigurationManager.AppSettings class which is
>> >> a part of system.configuration.dll without importing the namespace,
>> >> you'll need to use its full name :
>> >>
>> >> System.Configuration.System.Configuration.ConfigurationManager.AppSettings
>> >>
>> >> That because the System.Configuration.System.Configuration namespace
>> >> is *not* imported automatically into every aspx page.
>> >>
>> >> I hope this is clearer to you now.
>> >>
>> >>
>> >>
>> >> Juan T. Llibre, ASP.NET MVP
>> >> ASP.NET FAQ : http://asp.net.do/faq/
>> >> ASPNETFAQ.COM : http://www.aspnetfaq.com/
>> >> Foros de ASP.NET en Español : http://asp.net.do/foros/
>> >> ======================================
>> >> "Greg Burns" <bluebunny@xxxxxxxxxxxxxxxxx> wrote in message
>> >> news:utHTBAN5FHA.252@xxxxxxxxxxxxxxxxxxxxxxx
>> >> > FYI, to actually use System.Configuration.ConfigurationManager.AppSettings in 2.0
>> >> > you
>> >> > must add a reference to System.configuration.dll. (I wasted a lot of time trying
>> >> > to
>> >> > figure that out).
>> >> >
>> >> > This is not necessary to do to use
>> >> > System.Configuration.ConfigurationSettings.AppSettings and I not sure why. Can
>> >> > anyone
>> >> > explain?
>> >> >
>> >> > Greg
>> >> >
>> >> > "Jaime Stuardo" <jstuardo@xxxxxxxxxxxxx> wrote in message
>> >> > news:uMErc0M5FHA.884@xxxxxxxxxxxxxxxxxxxxxxx
>> >> >> Hi Patrick..
>> >> >>
>> >> >> I cannot do that since this method is in a DLL that is referenced by the ASP.NET
>> >> >> project. When I use AppSettings from the component, I receive a null. However I
>> >> >> think I
>> >> >> could use what you suggested if I have public DataSet GetCategories(String
>> >> >> connectionStr).
>> >> >>
>> >> >> By the way, I use ASP.NET 2.0 and in this version
>> >> >> ConfigurationSettings.AppSettings
>> >> >> is
>> >> >> reported as obsolete. I should use ConfigurationManager.AppSettings instead.
>> >> >>
>> >> >> Jaime
>> >> >>
>> >> >> "Patrick.O.Ige" <naijacoder@xxxxxxxxxxx> wrote in message
>> >> >> news:emOmcoM5FHA.2364@xxxxxxxxxxxxxxxxxxxxxxx
>> >> >>> if you have it in Web.Config like:-
>> >> >>> <add key="ConnectionString" value="server=(local);database=YourDB;integrated
>> >> >>> security=true;" />
>> >> >>> Then in you app do:-
>> >> >>> myconnection = New
>> >> >>> SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
>> >> >>> Is that what you are looking for.
>> >> >>> Patrick
>> >> >>>
>> >> >>>
>> >> >>> "Jaime Stuardo" <jstuardo@xxxxxxxxxxxxx> wrote in message
>> >> >>> news:efEeFlM5FHA.4012@xxxxxxxxxxxxxxxxxxxxxxx
>> >> >>>> Hi all..
>> >> >>>>
>> >> >>>> I have created a business logic component that is used from my ASP.NET
>> >> >>>> webform. It works, but connection string to the database is hard coded, as
>> >> >>>> in this method :
>> >> >>>>
>> >> >>>> public DataSet GetCategories()
>> >> >>>> {
>> >> >>>> SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial
>> >> >>>> Catalog=XXXX;User ID=X;Password=Y");
>> >> >>>> SqlDataAdapter adapter = new SqlDataAdapter("SELECT CAT_ID, CAT_NOM
>> >> >>> FROM
>> >> >>>> CATEGORIA ORDER BY CAT_NOM", conn);
>> >> >>>> DataSet ds = new DataSet();
>> >> >>>> adapter.Fill(ds);
>> >> >>>> return ds;
>> >> >>>> }
>> >> >>>>
>> >> >>>> I need to know what is the best way to pass the connection string that is
>> >> >>>> present in the web.config file. I tried using a constructor in that
>> >> >>> business
>> >> >>>> class but with no success. Also I have read that ObjectDataSource uses
>> >> >>>> stateless components so I think the usage of properties isn't allowed
>> >> >>> here.
>> >> >>>>
>> >> >>>> The question is, the only solution I could implement is passing the
>> >> >>>> connection string as a parameter to GetCategories? is there another one?
>> >> >>>> Since this method resides in a DLL, I cannot use ConnectionManager class
>> >> >>> to
>> >> >>>> access web.config file.
>> >> >>>>
>> >> >>>> Thanks a lot
>> >> >>>> Jaime
>> >> >>>>
>> >> >>>>
>> >> >>>
>> >> >>>
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>
.
- References:
- Re: Passing the connection string from a web form to a business lo
- From: Juan T. Llibre
- Re: Passing the connection string from a web form to a business lo
- Prev by Date: Re: Should I explicitly destroy objects
- Next by Date: Re: In asp.net, .css file is encouraged to use?
- Previous by thread: Re: Passing the connection string from a web form to a business lo
- Next by thread: Re: Passing the connection string from a web form to a business lo
- Index(es):
Relevant Pages
|