PC Review


Reply
Thread Tools Rate Thread

Correct usage of ADO.Net in an ASP.Net environment

 
 
Heath Kelly
Guest
Posts: n/a
 
      15th Jul 2003
I need advice on correct usage of ADO.NET in an ASP.Net environment.
I have an ASP.Net application that accesses data through a referenced
class library. Things start to break down when multiple web clients
attempt to use the application at the same time. A common error that
gets returned is "sqlcommand is currently busy open, fetching".
I can't understand why the users might be using the same instance of
my connection, but this error message would seem to indicate that they
are.

Below is an outline of my applications architecture.
I would appreciate any advice or suggestions as to what I might change
in my approach to get my ASP.Net application connecting to data in a
multiuser environment.

Classes that contains my business logic:

Public Class AAA
Private CON As New SqlClient.SqlConnection()
Private RDR As SqlClient.SqlDataReader
Private CMD As New SqlClient.SqlCommand()

Public Sub DoStuff
Common.ConnectDatabase(CON, "Staff")
CMD.CommandText = "SELECT * FROM tblStaff"
CMD.Connection = CON
RDR = CMD.ExecuteReader
If RDR.Read = False Then
...
...
End If
RDR.Close()
CON.Close()
End Sub
End Class

This module lives in the same dll as my business logic and contains
commonly used stuff like connecting to databases:

Public Module Common
Public Function ConnectDatabase(ByRef CON As SqlClient.SqlConnection,
ByVal sDatabase As String) As Boolean
Try
Select Case sDatabase
Case "Staff"
CON = New System.Data.SqlClient.SqlConnection()
CON.ConnectionString = "Integrated
Security=False;User ID=;Password=;Initial Catalog=Staff;Data
Source=myserver;"
CON.Open()
End Select
Catch
Return False
End Try
Return True
End Function
End Class

Now, my ASP.Net web pages interact with the above code like this:
(Note - all my code is in code behind. This is an example from an
aspx
page called Staff.aspx)

Public Class Staff
Protected MyClass as New AAA()

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PrintStaff()
End Sub

Private Sub PrintStaff()
MyClass.DoStuff
End Sub
End Class

The philosophy is that everytime a method in my classes is called a
connection is opened, actions are performed, then the connection is
closed. Note that I do not "dispose" of the collection so it should
be returned to the pool. One of the things I am trying to achieve by
following this approach is to take any concern for attaching to
databases away from the coder building the ASP.Net pages. The classes
perform all data activity.
 
Reply With Quote
 
 
 
 
Kathleen Dollard
Guest
Posts: n/a
 
      16th Jul 2003
Heath,

Do the easy stuff first. Put the close in a Try/FInally block, making sure
the connection is open or wrapping the close in another Try/Catch.

Kathleen


"Heath Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I need advice on correct usage of ADO.NET in an ASP.Net environment.
> I have an ASP.Net application that accesses data through a referenced
> class library. Things start to break down when multiple web clients
> attempt to use the application at the same time. A common error that
> gets returned is "sqlcommand is currently busy open, fetching".
> I can't understand why the users might be using the same instance of
> my connection, but this error message would seem to indicate that they
> are.
>
> Below is an outline of my applications architecture.
> I would appreciate any advice or suggestions as to what I might change
> in my approach to get my ASP.Net application connecting to data in a
> multiuser environment.
>
> Classes that contains my business logic:
>
> Public Class AAA
> Private CON As New SqlClient.SqlConnection()
> Private RDR As SqlClient.SqlDataReader
> Private CMD As New SqlClient.SqlCommand()
>
> Public Sub DoStuff
> Common.ConnectDatabase(CON, "Staff")
> CMD.CommandText = "SELECT * FROM tblStaff"
> CMD.Connection = CON
> RDR = CMD.ExecuteReader
> If RDR.Read = False Then
> ...
> ...
> End If
> RDR.Close()
> CON.Close()
> End Sub
> End Class
>
> This module lives in the same dll as my business logic and contains
> commonly used stuff like connecting to databases:
>
> Public Module Common
> Public Function ConnectDatabase(ByRef CON As SqlClient.SqlConnection,
> ByVal sDatabase As String) As Boolean
> Try
> Select Case sDatabase
> Case "Staff"
> CON = New System.Data.SqlClient.SqlConnection()
> CON.ConnectionString = "Integrated
> Security=False;User ID=;Password=;Initial Catalog=Staff;Data
> Source=myserver;"
> CON.Open()
> End Select
> Catch
> Return False
> End Try
> Return True
> End Function
> End Class
>
> Now, my ASP.Net web pages interact with the above code like this:
> (Note - all my code is in code behind. This is an example from an
> aspx
> page called Staff.aspx)
>
> Public Class Staff
> Protected MyClass as New AAA()
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> PrintStaff()
> End Sub
>
> Private Sub PrintStaff()
> MyClass.DoStuff
> End Sub
> End Class
>
> The philosophy is that everytime a method in my classes is called a
> connection is opened, actions are performed, then the connection is
> closed. Note that I do not "dispose" of the collection so it should
> be returned to the pool. One of the things I am trying to achieve by
> following this approach is to take any concern for attaching to
> databases away from the coder building the ASP.Net pages. The classes
> perform all data activity.



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Correct usage of DecryptDocument Harry K. Microsoft C# .NET 0 4th Jan 2009 06:35 PM
The correct usage of your and you're =?Utf-8?B?U2VhbjNERA==?= Microsoft Word Document Management 2 29th Jun 2007 02:08 PM
Usage of environment variable in a batch file as command line argument kk Microsoft Windows 2000 CMD Promt 2 28th Jan 2006 06:33 AM
Correct Usage of Collections? =?Utf-8?B?VGltIFQu?= Microsoft VB .NET 1 28th Apr 2005 12:01 AM
Re: Correct usage of ADO.Net in an ASP.Net environment Natty Gur Microsoft ASP .NET 2 21st Jul 2003 03:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:29 PM.