473,545 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET Question: Any problems with loading a SqlConnection into the current HttpContext?

Does anyone see any problem with the loading a SqlConnection into the
System.Web.Http ContextCurrent. Items collection in a Page's Constructor. Then
Closing and Disposing of it the OnUnload method?

Connection would not be "Opened" until it was actually used (this is handled
by a common "Helper" class - similar to Microsoft's SqlHelper Application
Block), Ie:

public class MyPage : Page {
public MyPage() {
System.Web.Http Context.Current .Items["currentConnect ion"] = new
SqlConnection() ;

((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
onnectionString = "connection_str ing_here";
}

... Object calls here to SELECT/INSERT/UPDATE data using the common
connection

override protected void OnUnload(EventA rgs args) {

((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
lose();

((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).D
ispose();

base.OnUnload(a rgs);
}
}

This could allow other objects to use this connection and would greatly
simplify connection usage.
Could also use the same exact concept for a Transaction that is used in
multiple objects that know nothing about each other, ie:
(each Save method below uses the Connection and Transaction objects stored
in System.Web.Http Context.Current .Items)

public SaveData() {
System.Web.Http Context.Current .Items["currentTransac tion"]
=
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).B
eginTransaction ();

try {
ClientObject.Sa ve();

AddressObject.S ave(ClientObjec t.ID);
PhoneObject.Sav e(ClientObject. ID);
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Co mmit();
} catch (Exception exp) {

((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Ro llback();
throw(exp);
} finally {

((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Di spose();
}
}
Does anyone see any major performance issues or other potential problems
with this? Thanks for any input!

Nayt Grochowski


Nov 15 '05 #1
0 1714

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4803
by: Asbjørn Ulsberg | last post by:
Sorry for posting to a lot of groups, but I'm not sure what this problem relates to, so I thought it was better to be safe than sorry. Please feel free to set FUT to the proper group when answering. I'm having problems with the XSLT function 'document()' after upgrading to ASP.NET 1.1. I've started using XmlUrlResolver in the...
1
2278
by: Nayt Grochowski | last post by:
Does anyone see any problem with the loading a SqlConnection into the System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then Closing and Disposing of it the OnUnload method? Connection would not be "Opened" until it was actually used (this is handled by a common "Helper" class - similar to Microsoft's SqlHelper...
2
1098
by: Paul M | last post by:
hi there, i have an <asp:button> control on my webpage that when clicked, i want to redirect the HTTP request to an EXE file, but instead of actually directing the browser and address bar to there, i only want the SaveAs box to appear, to download the file, and the actual web page to remain the same. VB.NET example would be greatly...
2
2278
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 In the current release of our system, we decided to "wrap" the ASP.NET Session and Application objects to improve code clarity and accuracy. For...
0
964
by: ujjc001 | last post by:
Here's the layout. Aspx has a repeater on the left, which will call a function to change a session var that holds the usercontrol name to add (i.e. "uc_myusercontrol.ascx") and then calls default.aspx which in page init, loads the control. The problem is that if the user clicks on a link, the page starts to reload but the user clicks on...
0
1746
by: Rick Hein | last post by:
I've got a problem with an app I've been working on, the Caching object and events not firing correctly. In a nutshell: When I'm debugging, and I set a breakpoint in the removed item call back, the code works correctly. If there is no debugger it doesn't fire, which causes the app to have problems. Here's what the code does: The...
4
2566
by: =?iso-8859-1?B?Sm9oYW4gU2r2c3Ry9m0=?= | last post by:
It seems that many people experience problems with mix-ups and non-intentional sharing of sessions in ASP.NET. This is often tracked down to the use of static variables for user data, which are indeed shared and overwritten among all users. I have this problem as well in one of my applications. However, there are no static variables, but...
5
1299
by: ThunderMusic | last post by:
Hi, I always refer to this page to know the order of events in a page : http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx but this time, I'm mystified... I have a Control called "ImagePreloader" I placed in the head of my MasterPage (aspx)... in the Init of the "ImagePreloader", I put the instance in the...
3
13431
by: Brett R. Wesoloski | last post by:
I am having problems using HttpContext.Current.Request.Url.Host. I have some code that does this... if (HttpContext.Current != null) { subdomain = HttpContext.Current.Request.Url.Host; }
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.