Global.asax not working?

I’m working my way through “Build Your Own ASP.NET Website Using C# & VB.NET” (April 2004 version) and I’ve come to Chapter 11: Web Applications. I got to the section “Working with the Global.asax File”, and I’ve hit a snag. I typed up the Global.asax file and the index.aspx files, but when I view the index.aspx file through the browser, nothing is displayed. Even using the code from the Code Archive doesn’t seem to work. I get nothing but a big white blank page in my browser. Any ideas as to what may be wrong here?

Can you provide the code of the pages in question?

Did you create the application within IIS?

Westmich: I’d provide the code, but as it’s not in the first four chapters, I don’t know if Zak would appreciate that. However, if he’s okay with it, he or I can post that code, though I would be more comfortable with him posting it, as it is his book.

Zak: I have created a folder called “practice” within IIS and that’s where I’ve been storing all of the code I have produced while going through the book. When I got to Chapter 11, I followed the instructions at the start and defined the practice folder as an IIS application. However, global.asax does not seem to do anything. Like I said, I don’t get any error messages, just a blank page. It’s like the page works, but doesn’t pull any info from the database or something.

When a blank page is displayed it usually means your events are not wired. Make sure the events are wired correctly.

Also I would not worry about posting the code. Others have done it in the past and there are no rules against it afaik.

Yeah, go ahead and post it.

OK. Here it is. First, the Global.asax file:

<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>

<script runat="server" language="VB">
Sub Application_Start(sender As Object, e As EventArgs)
	Dim objConn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\\Inetpub\\wwwroot\\practice\\Dorknozzle\\Database\\Dorknozzle.mdb")
	Dim objDA As OleDbDataAdapter
	Dim objDS As New DataSet()
	
	objDA = New OleDbDataAdapter("SELECT * FROM EmployeeStore", objConn)
	objDA.Fill(objDS, "EmployeeStore")
	
	Application("EmployeeStoreItems") = objDS
End Sub
</script>

And here’s the index.aspx file:

<html>
<head>
<title>Application_Start Event</title>
<script language="VB" runat="server">
Sub Page_Load()
	dgEmployeeStoreItems.DataSource = Application("EmployeeStoreItems")
	dgEmployeeStoreItems.DataBind()
End Sub
</script>
</head>
<body>
<asp:DataGrid id="dgEmployeeStoreItems" runat="server" />
</body>
</html>

The files are saved in C:\Inetpub\wwwroot\practice\Dorknozzle\

Your code worked fine for me. You sure the file path is correct to your database?

That is the correct path. And it’s the same one I’ve used for all the other pages and they all work. Could my Application Settings be wrong, perhaps? When I open IIS and look at the Properties of “practice”, on the Directory tab I see a space called “Application Settings”. They are:

Application Name: practice
Starting point: <Default Web … \practice
Execute permissions: Scripts only
Application protection: Medium (Pooled)

Same settings as mine. That is odd, try re-creating the directory as an application. Maybe Zak can shed some light.

I found out what the problem was. While I had the folder “Practice” as an application, the global.asax and related index.aspx page was in a sub-folder of Practice and was not set as an application. I went into IIS and created the sub-folder as an application and everything worked.

I thought that making the “Practice” folder an application would make all of its subfolders applications as well, but apparently this is not so. Is there a setting I can set that will make all of a folder’s sub-folders become applications when the parent folder is made an application?

Dont know the answer to your second question, but I think if you move the global.asax file into the root directory “Practice” then you wouldn’t have the problem you’re having.

I believe I have done this before and it worked fine, although if I’m wrong, someone will point it out.

I am up to the same point. I was wondering… Shouldn’t there be objConn.Open() and objConn.Close() in that global.asax file?

Also, I get this error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30519: Overload resolution failed because no accessible ‘New’ can be called without a narrowing conversion:

Source Error:

Line 9:
Line 10: objConn.Open()
Line 11: objDA = New SqlDataAdapter(“SELECT * FROM EmployeeStore”, objConn)
Line 12: objDA.Fill(objDS, “EmployeeStore”)
Line 13: objConn.Close()

Source File: E:\\CodeExamples\global.asax Line: 11

You don’t need a connection object. You are using Microsoft’s new method of disconnected data. With a SqlDataAdapter, this goes out to the data source, the database, queries the database, then pulls the contents from the db into the dataset, which is a virtual database in memory on your local machine.

So change your code from this:

objConn.Open()
objDA = New SqlDataAdapter(“SELECT * FROM EmployeeStore”, objConn)
objDA.Fill(objDS, “EmployeeStore”)
objConn.Close()

To this:

objDA= New SqlDataAdapter(“SELECT * FROM EmployeeStore”, objConn)
objDA.Fill(objDS, “EmployeeStore”)

That should fix you up.

I took out the connection object but I still get the same error message on the browser.

BC30519: Overload resolution failed because no accessible ‘New’ can be called without a narrowing conversion:

on this line -> objDA = New SqlDataAdapter(“SELECT * FROM EmployeeStore”, objConn)

You still need to define your data source with the variable objConn. Post your code for global.asax and index.aspx file.

OK Here goes:

Global.asax:

<%@ Import Namespace=“System.Data.SqlClient” %>
<%@ Import Namespace=“System.Data” %>

<script runat=“server” language=“VB”>
Sub Application_Start(sender As Object, e As EventArgs)
Dim objConn = New SqlConnection(“Server=danhome
etsdk;Database=Dorknozzle;User ID=sa;Password=woohyunlee”)
Dim objDA As SqlDataAdapter
Dim objDS As New DataSet()

	objDA = New SqlDataAdapter("SELECT * FROM EmployeeStore", objConn)
	objDA.Fill(objDS, "EmployeeStore")

	Application("EmployeeStoreItems") = objDS
End Sub

</script>

index.aspx:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title> Testing Global.asax </title>
<script runat=“server” language=“VB”>
Sub Page_Load()
dgEmployStoreItems.DataSource = Application(“EmployeeStoreItems”)
dgEmployStoreItems.DataBind()
End Sub
</script>
</head>

<body>
<asp:DataGrid id=“dgEmployStoreItems” runat=“server” />
</body>
</html>

I am having the same problem - Global.asax not working. I have a Virtual Directory
called WebDocs pointing to a folder ASPApplication in C:\. Global.asax and index.aspx
both are in ASPApplication folder. But still it does not work!

Any ideas!!!

Leo: Your code is working for me. The only thing I changed were the database namespace, I un-installed MSDE the other day because it was slowing down my pc. Are you sure the global.asax file is in the root directory of your application? For example, you files are in C:\Dorknozzle, then global.asax needs to be in the root directory of this, plus, this directory needs to be an application within IIS.

Kel: Same thing, are you sure the files are within a folder that is an application within IIS? If so, post your code to make sure its ok.

Here’s the code from Leo that works for me, minus the Sql Namespaces:

Global.asax


<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>

<script runat="server" language="VB">
Sub Application_Start(sender As Object, e As EventArgs)
Dim objConn as New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\\CO\\Dorknozzle.mdb;")
Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()

objDA = New OleDbDataAdapter("SELECT * FROM EmployeeStore", objConn)
objDA.Fill(objDS, "EmployeeStore")

Application("EmployeeStoreItems") = objDS
End Sub
</script>

index.aspx


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Testing Global.asax </title>
<script runat="server" language="VB">
Sub Page_Load()
dgEmployStoreItems.DataSource = Application("EmployeeStoreItems")
dgEmployStoreItems.DataBind()
End Sub
</script>
</head>

<body>
<asp:DataGrid id="dgEmployStoreItems" runat="server" />
</body>
</html>

See how these suggestions work.

I have setup my virtual directories as follows:

Default Web Site -> Projects -> CodeExamples

Both Projects and CodeExamples are set up as applications. global.asax and index.aspx files are in CodeExamples root folder. The reason for this is that I have several projects. One being the CodeExamples.