Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Re: connection string in global.asax


Message #1 by "Franck LOKOSSI" <flokossi@a...> on Wed, 22 Jan 2003 16:24:28
Hi Franck.

I would like to know if you found an answer to your problem ?

D@v...


> 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
Message #2 by "Rohit Arora" <rohit_arora@i...> on Thu, 23 Jan 2003 09:53:02 +0530
the better way is to specify the connectionstring in web.config.

eg:

<appSettings>
<add key="connectionstring" value="server=111.111.111.111; initial
catalog=abc; uid=sa; pwd=sa;"/>
</appSettings>

and in ur code use it like

string strConn = Configuration.appSettings["connectionstring"];

hope it works
regards
rohit

-----Original Message-----
From: Franck LOKOSSI [mailto:flokossi@a...]
Sent: Wednesday, January 22, 2003 4:24 PM
To: ASP.NET
Subject: [aspx] Re: connection string in global.asax


Hi Franck.

I would like to know if you found an answer to your problem ?

D@v...


> 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