Global ASAX

P

PRTC

I'm trying to use the global.asax in my new web aplication proyect
using the Application start to store my connection string

GLOBAL.ASAX.vb


Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim objConnection As OleDbConnection
Dim daContent As OleDbDataAdapter
Dim objDataReader As OleDbDataReader
Dim objCommand As New OleDbCommand
Dim mySqlStatment As String = "Select * FROM materia1Links_cl"
objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=D:\works\WEBS\edusalon\dbcontainer\datdb.mdb")
End Sub

The problem is, when I try to use this code in the

Webform1.aspx

Code inside of the HTML

<Script language="vb" runat ="server"

DIM mySqlStatment AS STRING = "Select * FROM materia1Links_cl"

try

objConnection.open()
objDataReader = objCommand.ExecuteReader()

DO WHILE objDataReader.Read()= true

Response.write(objDataReader("mycolum"))

loop

objDataReader.close()
objConnection.close()
Catch e as EXCEPTION

end try
end Sub

</script>

The error generated are my variables ar not declare objConnection,
objDataReader ... I m not really sure what I'm doing wrong
I thoght that when I place my variables in the globlas asax thay going to be
available each time I open the web application
did somebody know how is the correct way to use the Application_Start with
my connection string in it and how my others form
can use it. thanks
 
K

Karl Seguin

PRTC,
You are misunderstanding the relationship of what's happening. between the
global.asax and pages. Both of these are self-contained classes, objects
defined in one won't be automatically accessible to the other. That is the
objConnection variable you are defining in Application_Start is scoped to
that function only. Other functions (like Application_End) within
global.asax won't even be able to access it, let alone in a separate
class/page.

If you wanted to do something, you would use the Application variable to
store values and make them accessible throughout your classes/pages

Application_Start
Application.Add("Connection", new OleDbConnection("...."))
end sub

and in your code inside the HTML,

OleDbConnection connection = ctype(Application("Connection"),
OleDbConnection)


Now having said all of this, this isn't the right way to go about handling
connections. You would be better off using a data access layer which takes
care of all your database interactions, as opposed to letting your html code
do that kind of thing. Even if you don't go for that, you probably only
want to store the connection string in the Application variable and create
new instances of OleDbConnection as needed...having instances of objects
floating around isn't all that pretty..

Karl
 
T

THECO

Thanks for the help thats what Im looking for, trying to understand the use
of
Global.asax
 

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

Top