Passing the connection string from a web form to a business logic component

J

Jaime Stuardo

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
 
P

Patrick.O.Ige

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
 
B

Bruce Barker

the best solution is to write you own application setting class (use
interfaces). this allows multiple implementation of loading settings, but
one for reading.

hint: use the factory model to allow a static create method.

-- bruce (sqlwork.com)
 
J

Jaime Stuardo

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
 
G

Greg Burns

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
 
J

Juan T. Llibre

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.
 
G

Guest

'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"];
 
J

Juan T. Llibre

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"];





Mike L said:
'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 said:
Hi, Greg.

re:

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.
 
G

Greg Burns

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.

Not really. I guess I'm a little dense. :^)

This, of course, compiles just fine:
Dim sConnString As String =
System.Configuration.ConfigurationManager.AppSettings("dsn")

and this (because System is imported by default in VB)
Dim sConnString As String =
Configuration.ConfigurationManager.AppSettings("dsn")

and if I import System.Configuation I would just use:
Imports System.Configuration
Dim sConnString As String = ConfigurationManager.AppSettings("dsn")

but now this won't compile: ?
Imports System.Configuration
Dim sConnString As String =
Configuration.ConfigurationManager.AppSettings("dsn")
Error: 'System' is not a member of 'System.Configuration.Configuration'

And I can't get this line to compile at all...?
Dim sConnString As String =
System.Configuration.System.Configuration.ConfigurationManager.AppSettings("dsn")

Error: 'System' is not a member of 'Configuration'

I don't think I will ever completely understand this. Don't bother trying to
explain it to me again, I'm hopeless. :^)
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"];

Interesting, never heard that metioned before. I thought we were expected
to stop using System.Configuration.ConfigurationSettings.AppSettings?

Greg
 
G

Guest

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 said:
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"];





Mike L said:
'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 said:
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.




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

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

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


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
 
J

Juan T. Llibre

Well, other than an obvious typo, it's a sound explanation.

If you need to verify, download Lutz Roeder's Reflector:
http://www.aisto.com/roeder/dotnet/
and take a look at both assemblies : System.dll and System.configuration.dll

You'll understand what I meant immediately.




Greg Burns said:
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.

Not really. I guess I'm a little dense. :^)

This, of course, compiles just fine:
Dim sConnString As String = System.Configuration.ConfigurationManager.AppSettings("dsn")

and this (because System is imported by default in VB)
Dim sConnString As String = Configuration.ConfigurationManager.AppSettings("dsn")

and if I import System.Configuation I would just use:
Imports System.Configuration
Dim sConnString As String = ConfigurationManager.AppSettings("dsn")

but now this won't compile: ?
Imports System.Configuration
Dim sConnString As String = Configuration.ConfigurationManager.AppSettings("dsn")
Error: 'System' is not a member of 'System.Configuration.Configuration'

And I can't get this line to compile at all...?
Dim sConnString As String =
System.Configuration.System.Configuration.ConfigurationManager.AppSettings("dsn")

Error: 'System' is not a member of 'Configuration'

I don't think I will ever completely understand this. Don't bother trying to explain it
to me again, I'm hopeless. :^)
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"];

Interesting, never heard that metioned before. I thought we were expected to stop using
System.Configuration.ConfigurationSettings.AppSettings?

Greg
 
J

Juan T. Llibre

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.





Mike L said:
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 said:
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"];





Mike L said:
'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"];


:

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.




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

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

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


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
 
G

Greg Burns

Juan T. Llibre said:
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.

ACK.

But what about his question?
Should I ignore the warring messages for "string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"];" ?

Sounds like you are saying you should ingore the warnings and continue to
use ConfigurationSettings.AppSettings for reading from web.config and
app.config files. This is the first time I've heard somebody suggest that.
I was under the impressions that ConfigurationSettings.AppSettings was
obsolete and should be avoided in favor of the new
ConfigurationManager.AppSettings.

Greg
 
J

Juan T. Llibre

re:
ConfigurationSettings.AppSettings was obsolete and should be avoided in favor of the new
ConfigurationManager.AppSettings

It is *deprecated*, not obsolete.

You can *still* read web.config values with ConfigurationSettings.AppSettings.

At some point in the future, though, it will stop working. (.Net Framework 3.0 ?)

re:
Sounds like you are saying you should ingore the warnings and continue to use
ConfigurationSettings.AppSettings for reading from web.config and app.config files.

Like I said, app.config is for Windows Forms,
and web.config is for web applications.

He said he was using app.config, but this is a web applications newsgroup.

If he is getting warnings when he uses app.config in a web application,
then he should, simply, only use app.config for Windows applications,
and use web.config for web applications.

Otherwise, if he is using app.config in a Windows Application,
he should post to the right newsgroup for Windows apps, which is :

microsoft.public.dotnet.framework.windowsforms or any of its subgroups.

Is that clearer now ?




Greg Burns said:
Juan T. Llibre said:
Isn't System.Configuration.ConfigurationSettings.AppSettings
hard-wired to read from web.config if you're writing a web application?
re:
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.

ACK.

But what about his question?
Should I ignore the warring messages for "string sConnString =
System.Configuration.ConfigurationSettings.AppSettings["dsn"];" ?

Sounds like you are saying you should ingore the warnings and continue to use
ConfigurationSettings.AppSettings for reading from web.config and app.config files.
This is the first time I've heard somebody suggest that. I was under the impressions
that ConfigurationSettings.AppSettings was obsolete and should be avoided in favor of
the new ConfigurationManager.AppSettings.

Greg
 
G

Greg Burns

inline

Juan T. Llibre said:
re:

It is *deprecated*, not obsolete.

The warning VS gives is "obsolete".
You can *still* read web.config values with
ConfigurationSettings.AppSettings.

At some point in the future, though, it will stop working. (.Net Framework
3.0 ?)

re:

Like I said, app.config is for Windows Forms,
and web.config is for web applications.

Yes, of course.
He said he was using app.config, but this is a web applications newsgroup.

If he is getting warnings when he uses app.config in a web application,
then he should, simply, only use app.config for Windows applications,
and use web.config for web applications.

You get the warning in both Windows apps and Web apps when you use
ConfigurationSettings.AppSettings

Greg
 
J

Juan T. Llibre

re:
The warning VS gives is "obsolete".

It's still deprecated, not obsolete.

When something is obsolete, it doesn't works any more.
When something is deprecated, it still works.

re:
You get the warning in both Windows apps and Web apps when you use
ConfigurationSettings.AppSettings

I *never* get that warning neither in 1.1 nor in 2.0 web apps.
Maybe I'm just lucky ?

See an example at my test application, configured for ASP.NET 2.0:
http://asp.net.do/test/checkAppSettings.aspx

All that page has is :

checkAppSettings.aspx :
-------------------------
<script language="vb" runat="server">
Sub Page_Load()
Dim miSELECT as String = (ConfigurationSettings.AppSettings("selectDocs"))
setting.text = miSELECT
Dim miVersion as String = System.Environment.Version.ToString()
version.Text = "This application is running under ASP.NET version " & miVersion
End Sub
</script>
<html>
<head><title>Retrieve AppSetting key and ASP.NET Version</title></head>
<body>
<asp:label id="setting" runat="server" /><br/>
<asp:label id="version" runat="server" /><br/>
</body>
</html>
------------------------

The key in web.config reads like this :

<appSettings>
<add key="selectDocs" value="SELECT * FROM documents WHERE thereisdata ORDERBY
CreationDate DESC"/>
</appSettings>

Why am I not getting any warnings,
and why does that page compile OK if, as you say :
You get the warning in both Windows apps and Web apps when you use
ConfigurationSettings.AppSettings

?
 
G

Greg Burns

Juan T. Llibre said:
re:

It's still deprecated, not obsolete.

When something is obsolete, it doesn't works any more.
When something is deprecated, it still works.

Okay, okay. :)
re:

I *never* get that warning neither in 1.1 nor in 2.0 web apps.
Maybe I'm just lucky ?

We'll you definately would never see it in 1.1, because this is a compiler
warning in VS 2005 for .NET 2.0.

No clue, why you don't see it in VS 2005. I see it in both my code bedhind
and in-line in the .aspx if I do like your example.

You must be lucky. :)

Did you perhaps turn off this warning in an option somewhere?

Greg
 
J

Juan T. Llibre

re:
We'll you definately would never see it in 1.1, because this is a compiler warning in VS
2005 for .NET 2.0.

It's a 2.0 app.

However, VS 2005, or the .Net compiler team, may be schizophrenic.

On the one hand VS 2005 says that a class is "obsolete",
but it runs OK and it compiles without errors.

Maybe the documentation guys flubbed this one ?

What the warning message should say is that the class
has been "deprecated", but that it compiles and runs OK.

When something is obsolete, it doesn't work any more.
When something is deprecated, it still works.

Not your fault, and not my fault, but I tend to take
these documentation flubs with a little grain of salt.

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top