using global.asax

  • Thread starter Thread starter Yunus Emre ALPÖZEN [MCAD.NET]
  • Start date Start date
Y

Yunus Emre ALPÖZEN [MCAD.NET]

Why u need to use global.asax for something like this????

It was in ASP times...

If you need to specify a connection string that can be accessed via any
page, you should use web.config file.
Get it from web .config at application startup and add it application cache.
Access your connection string via application cache. But try to redesign
your architecture as at least two tier application.... Don't access database
from web pages....
 
Hello,

On Microsoft Visual Studio .NET 2003,

I want to use some global elements,
that can be used in each one of my pages.
i.e I put a oleDBConnection on global.asax.vb

How can I use it (the oleDBConnection on global.asa.vb) at the other aspx
pages ?
Need sample code, please.

Thanks :)
 
So,

I don't understand - why not using global.asax ?
what you said is for database connection & accessing database : web.config
is preferred.
(If I need just simple temporary variables, I can use the global.asax).

I need a sample code, please.
(using connection, and accessing the database).

Thanks :)
 
.... besides - I think that web.config doesn't support *.mdb, but support sql
server - Is that true ?
(what I use is *.mdb)

Thanks :)
 
I agree with u this is a good example...
But i would like to add a few words on it...

At every time web.config file is changed application is restarted by asp.net
automatically my advice u to use global.asax at this point. Handle
applcation start event and add Application cache the values that u get from
web.config During the whole application use Application cache to access this
configuration parameters.

You can still use global.asax. But you should not !!!
 
Some clarifications :
--------------------------
1) For the code :
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
- Where shall I put it (not web.config ?)

2) I persume that global variables for db configuration, I best shall put it
on web.config.
Shall I put any of global variables on web.config (same method , <add
key = "mynewkey" value = "mynewvalue" />

Thanks :)
 
.... Also I curious whether on every aspx page I need to open & close the
connection ?

Thanks :)
 
We've had several discussions on this, Yunus.
Maybe you missed them.

In essence, it doesn't matter whether you put the
connection data in web.config or in global.asax.

It's much easier to retrieve static application data from web.config,
though, since there's built-in methods for getting that data from it.

If you use global.asax you have to roll your own methods.

Regarding what you state :
every time web.config file is changed application is restarted by asp.net
automatically my advice u to use global.asax at this point.

The application is also restarted by asp.net when global.asax is changed,
so I don't understand what you're trying to say and why that would
stop you from recommending using web.config.

Regarding :
Handle applcation start event and add Application cache the values that u get from
web.config

Why would you need to do that ?

ASP.NET *automatically* caches the contents of web.config for you
when the application starts, and everything in web.config is available
for retrieval from memory *without* having to cache it again.

Rethink this a bit, Yunus.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
I agree with u.. Using web.config is recommended as you can see from my
first post. And my answer says
"You can still use global.asax. But you should not !!!" It is possible to
implement every thing what web.config brings out. But u should not !!! U
should use web.config instead of global.asax. I think, i was
misunderstood...

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET
 
re:
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
- Where shall I put it (not web.config ?)

Either inline in your aspx page, or in your codebehind if you're using that.

Here's an inline example :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim DS As DataSet
Dim MyConnection As String
Dim MyCommand As SqlDataAdapter

MyConnection = ConfigurationSettings.AppSettings("connNorthwind")

MyCommand = New SqlDataAdapter("select * from orders where orderid = 10270", MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Orders")

Repeater.DataSource = DS
Repeater.DataBind()

End Sub

This assumes that your web.config entry looks like this :

<configuration>

<appSettings>
<add key="connNorthwind" value="server=(local);trusted_connection=true;database=northwind"/>
</appSettings>

Make sure you replace *your* SQL server's name in this string.

re:
2) I presume that global variables for db configuration,
I best shall put it on web.config.
Yes.

Shall I put any of global variables on web.config (same method ,
<add key = "mynewkey" value = "mynewvalue" />

Exactly.

Now, for a news update :

All this changes in ASP.NET 2.0. News at 2.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
That's O.K.
But, as I have mention earlier :

.... on every aspx page I need to open & close the
connection, if I do as your advise.

So I have solved it as follows :
------------------------------------------
If I put on session_start (global.asax.vb) the code :
Session("conMain") = me.conMain
(me.conMain is oleDBConnection).

I can reference on every of my aspx pages on their page_load event as following :
OleDBCommand1.Connection = Session("conMain").
(oleDBCommand1 is oleDBCommand).
----------------------------------------------------------------------
.... which is much easier to maintenance

One thing is that on global.asax.vb the event : InitializeComponent is called twice, but only when first running the application
(If I run the application again : InitializeComponent isn't called).
And I don't understand why.

Another thing is that I was advised (former to this forum) not using global.asax.vb for using Session("conMain") -
Another thing I don't understand why.

Is my solution has any problem using it ?

Thanks :)

Juan T. Llibre said:
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
- Where shall I put it (not web.config ?)

Either inline in your aspx page, or in your codebehind if you're using that.

Here's an inline example :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim DS As DataSet
Dim MyConnection As String
Dim MyCommand As SqlDataAdapter

MyConnection = ConfigurationSettings.AppSettings("connNorthwind")

MyCommand = New SqlDataAdapter("select * from orders where orderid = 10270", MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Orders")

Repeater.DataSource = DS
Repeater.DataBind()

End Sub

This assumes that your web.config entry looks like this :

<configuration>

<appSettings>
<add key="connNorthwind" value="server=(local);trusted_connection=true;database=northwind"/>
</appSettings>

Make sure you replace *your* SQL server's name in this string.

re:
2) I presume that global variables for db configuration,
I best shall put it on web.config.
Yes.

Shall I put any of global variables on web.config (same method ,
<add key = "mynewkey" value = "mynewvalue" />

Exactly.

Now, for a news update :

All this changes in ASP.NET 2.0. News at 2.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
I think only one person would use your application at the same time... Or one call for one session. What happens a user opens two different pages at the same time...

It has too many problem... My advice u take a look at duwamish sample....

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

That's O.K.
But, as I have mention earlier :

... on every aspx page I need to open & close the
connection, if I do as your advise.

So I have solved it as follows :
------------------------------------------
If I put on session_start (global.asax.vb) the code :
Session("conMain") = me.conMain
(me.conMain is oleDBConnection).

I can reference on every of my aspx pages on their page_load event as following :
OleDBCommand1.Connection = Session("conMain").
(oleDBCommand1 is oleDBCommand).
----------------------------------------------------------------------
... which is much easier to maintenance

One thing is that on global.asax.vb the event : InitializeComponent is called twice, but only when first running the application
(If I run the application again : InitializeComponent isn't called).
And I don't understand why.

Another thing is that I was advised (former to this forum) not using global.asax.vb for using Session("conMain") -
Another thing I don't understand why.

Is my solution has any problem using it ?

Thanks :)

Juan T. Llibre said:
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
- Where shall I put it (not web.config ?)

Either inline in your aspx page, or in your codebehind if you're using that.

Here's an inline example :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim DS As DataSet
Dim MyConnection As String
Dim MyCommand As SqlDataAdapter

MyConnection = ConfigurationSettings.AppSettings("connNorthwind")

MyCommand = New SqlDataAdapter("select * from orders where orderid = 10270", MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Orders")

Repeater.DataSource = DS
Repeater.DataBind()

End Sub

This assumes that your web.config entry looks like this :

<configuration>

<appSettings>
<add key="connNorthwind" value="server=(local);trusted_connection=true;database=northwind"/>
</appSettings>

Make sure you replace *your* SQL server's name in this string.

re:
2) I presume that global variables for db configuration,
I best shall put it on web.config.
Yes.

Shall I put any of global variables on web.config (same method ,
<add key = "mynewkey" value = "mynewvalue" />

Exactly.

Now, for a news update :

All this changes in ASP.NET 2.0. News at 2.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 

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

Similar Threads

how to Get ID in global.asax page 17
Global.asax not firing 16
storing database connection strings 5
Global.asax or web.config 3
Global.asax / Global.asax.cs in v2 6
global.asax 5
Global.asax Variables 10
global.asax 3

Back
Top