Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: connection string in global.asax


Message #1 by "Frank Tichy" <tichy@s...> on Wed, 22 Aug 2001 12:39:52 +0200
Hi,



I'm trying to make a connection call in the global.asax of my c# project.

I've coded a namespace as follows:

--connection.cs--

using System;

using System.Data;

using System.Data.Common;

using System.Data.OleDb;

using System.Data.SqlTypes;

using System.Data.SqlClient;



namespace connection

{

	/// <summary>

	/// Connection String.

	/// </summary>

	public class Data

	{

		public bool GetConnection (ref SQLConnection strConn)

		{

			try

			{

				strConn = new

SQLConnection("server=myserver;uid=sa;pwd=;database=Northwind");

				strConn.Open();

				return true;

			}

			catch (Exception exp)

			{

				return false;

			}

		} //GetConnection ends here

	} //class ends here

} //namespace end here

--connection.cs end---

In the global.asax, after getting the connection I store it into the

application level variable, and use it through out my application

--global.asax--

using System;

using System.Collections;

using System.ComponentModel;

using System.Web;

using System.Web.SessionState;

using System.Collections.Specialized;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.ComponentModel.Design;

using System.Data;

using System.Data.Common;

using System.Data.OleDb;

using System.Data.SqlTypes;

using System.Data.SqlClient;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.ComponentModel;

using System.Web.Services;

using System.Runtime.CompilerServices;

using connection;





namespace csharp

{

	/// <summary>

	/// Summary description for Global.

	/// </summary>

	public class Global : System.Web.HttpApplication

	{

		public SQLConnection strConn;

		protected void Application_Start(Object sender, EventArgs e)

		{

			try

			{

				strConn = (SQLConnection ) Application.Get ("Connection") ;

				if ( strConn == null )

				{

					throw ( new Exception ("No Connection"));

				}

			}

			catch (Exception exp )

			{

				Connection.Data dataCn = new Connection.Data ();

				if (dataCn.GetConnection (ref strConn))

					Application.Add ("Connection",strConn); //Keep this connection in to a

Application level variable.

			}

		}



		public void Session_Start(Object sender, EventArgs e)

		{



		}



		protected void Application_BeginRequest(Object sender, EventArgs e)

		{



		}



		protected void Application_EndRequest(Object sender, EventArgs e)

		{



		}



		protected void Session_End(Object sender, EventArgs e)

		{



		}



		protected void Application_End(Object sender, EventArgs e)

		{



		}

	}

}

-- global.asax end --

Then I thought that I could use this connection in any file by just putting

this in a script block

<html>

<head><title></title>

<script language=c# runat=server>

private SQLConnection cn;

cn = ( SQLConnection ) Application.Get ("Connection");

</script>

</head>

.

.

.

All I've achieved so far is this nice error message:

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: CS1519: Invalid token '=' in class, struct, or

interface member declaration



thanks in advance

Frank Tichy


  Return to Index