application variables not loading

D

Dan

I'm not able to access my variables that I've set in my
global.asax. Here is my simple code. Note: Error message
is below.

[[ global.asax ]]

<%@ Page Language="c#" %>

<script language="c#" runat="server">

string DbAccessString = "Provider=Microsoft.Jet.OleDb.4.0;
Data
Source=E:\\Users\\canadianmoneysaver\\public_html\\testing\
\survey.mdb";

</script>


[[ db_access_aspx ]]

<%@ Page Language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script language="c#" runat="server">

void Page_Load()
{
string strConnection =
DbAccessString;
data_src.Text =
strConnection;
OleDbConnection
objConnection = new OleDbConnection(strConnection);
try
{

objConnection.Open();

con_open.Text = "Connection opened success.<br />";

objConnection.Close();

con_close.Text = "Connection closed<br />";
}
catch (Exception e)
{

con_open.Text = "Connection failed to open.<br />";

con_close.Text=e.ToString();
}
}

</script>

<html>
<head>
<title></title>
</head>
<body>

<h4>Testing data connection



<asp:Label id="data_src"
Text="" runat="server"/></h4>

<asp:Label id="con_open"
Text="" runat="server"/><br />

<asp:Label id="con_close"
Text="" runat="server"/><br />


PATH: <%= Server.MapPath("/testing")%> <br />


</body>
</html>


[[ ERROR MESSAGE ]]

Compiler Error Message: CS0103: The name 'DbAccessString'
does not exist in the class or
namespace 'ASP.db_access_aspx'
 
G

George Ter-Saakov

Make your variable public and static then you will be able to access it
Global.DbAccessString.

The Global is the class created behind your global.aspx. It's the same class
as any other class in C# so you need to treat it the same way.

You can not refer to variables declared in other classes without explicitly
providing instance of the class.

George.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top