Is there a way to do this with web.config?

B

Brian Simmons

Hi all,

So, I don't have to keep adding the following lines to each of my
code-behind pages:
using System.Data.Sql;
using System.Data.SqlClient;

Is there way in the web.config file to add a setting which will tell each
page to automatically include/use those 2 namespaces?

ASP.NET 2

Thanks,
Brian
 
J

Juan T. Llibre

re:
!> Is there way in the web.config file to add a setting which will tell
!> each page to automatically include/use those 2 namespaces?

Yes.

The namespaces element defines a collection of import directives to use during
assembly pre-compilation. This attribute corresponds to the @ Import directive
on an ASP.NET page.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<namespaces>
<add namespace ="System.IO" />
<add namespace="System.Text"/>
</namespaces>
</pages>
</system.web>
</configuration>

Now, you can use System.IO and System.Text in any page without specifically importing them.
 
J

John Saunders [MVP]

Juan T. Llibre said:
re:
!> Is there way in the web.config file to add a setting which will tell
!> each page to automatically include/use those 2 namespaces?

Yes.

The namespaces element defines a collection of import directives to use
during
assembly pre-compilation. This attribute corresponds to the @ Import
directive
on an ASP.NET page.

OTOH, you might consider separating your data access from your pages. The
need to reference SQL Server from so many pages may be considered a mild
hint.
 
J

Juan T. Llibre

re:
!> OTOH, you might consider separating your data access from your pages. The
!> need to reference SQL Server from so many pages may be considered a mild hint.

Indeed...

Whenever I need to do data access, I compile an assembly which I can reference in any page.
Doing that makes code much more manageable.

;-)
 
B

Brian Simmons

Yes, John, isn't that the beauty of programming, you have choices in the way
_you_ want to do things. And neither is "right" or "wrong", it's whatever
works for _you_ and your client.

I asked a simple question, thanks for the answer I was looking Juan.
 
J

Juan T. Llibre

re:
thanks for the answer I was looking Juan

You're quite welcome, Brian.

As it happened, just last week I had to research that,
so the timing on your question was impeccable.

;-)





Brian Simmons said:
Yes, John, isn't that the beauty of programming, you have choices in the way _you_ want to do
things. And neither is "right" or "wrong", it's whatever works for _you_ and your client.

I asked a simple question, thanks for the answer I was looking Juan.
 
S

Samuel R. Neff

Yes there are choices, but most certainly there are some wrong
choices.

Unless you're talking about a one-time-use prototype that you're going
to throw away in a week, I'd agree with John that the db code should
be separate from the aspx code.

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



Yes, John, isn't that the beauty of programming, you have choices in the way
_you_ want to do things. And neither is "right" or "wrong", it's whatever
works for _you_ and your client.

I asked a simple question, thanks for the answer I was looking Juan.
 
B

Brian Simmons

Yes there are choices, but most certainly there are some wrong

Your opinion Sam and let's leave it at that.

I asked a simple question, got my answer, and am moving on.

Samuel R. Neff said:
Yes there are choices, but most certainly there are some wrong
choices.

Unless you're talking about a one-time-use prototype that you're going
to throw away in a week, I'd agree with John that the db code should
be separate from the aspx code.

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 

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

Forum statistics

Threads
473,999
Messages
2,570,245
Members
46,839
Latest member
MartinaBur

Latest Threads

Top