473,474 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Datareader problem

I have an application that constantly needs to read and write data to a
SQL box. For most of my read transactions I use a datareader. I
compiled a class that has all my datareader commands in it etc.

This works well, but every now and then a bunch of errors occurs,
relating to either the connection not being initialized, or reader
already associated with existing connection.

Now I have done everything I could think of to get rid of these errors,
but they still happen. Please note that the application is heavily
used, and at any time there can be up to 70 users doing something on
the system that will use the same datareader command in the class.

Here is the code for the property that gets used the most, and also
errors the most in the class:

Public ReadOnly Property SQLSelectCommand(ByVal SQLSelectString
As String, ByVal ReturnTableName As String) As drTypes
Get
Try
dTypes = New drTypes(ReturnTableName)
If sqlCon.State = ConnectionState.Closed Or
sqlCon.State = ConnectionState.Broken Then
sqlCon.Open()
End If
sqlCom = New SqlCommand(SQLSelectString, sqlCon)
If Not sqlDR Is Nothing Then
If sqlDR.IsClosed = False Then
sqlDR.Close()
End If
End If
sqlDR = sqlCom.ExecuteReader()
dt = New DataTable(ReturnTableName)
Dim y As Integer = 0
While sqlDR.Read
Dim i As Integer = 0, z As Integer = 0
i = sqlDR.FieldCount
Do While y < i
If Not
dt.Columns.Contains(sqlDR.GetName(y)).Equals(True) Then
dt.Columns.Add(sqlDR.GetName(y),
sqlDR.GetFieldType(y))
Else
'*column already exits
dt.Columns.Add(sqlDR.GetName(y) & "_" &
y, sqlDR.GetFieldType(y))
End If
y += 1
Loop
dr = dt.NewRow()
Do While z < i
dr(z) = sqlDR(z)
z += 1
Loop
dt.Rows.Add(dr)
End While
sqlDR.Close()
If sqlCon.State = ConnectionState.Open Or
sqlCon.State = ConnectionState.Connecting Or sqlCon.State =
ConnectionState.Executing Or sqlCon.State = ConnectionState.Fetching
Then
sqlCon.Close()
sqlCon.Dispose()
End If
dTypes.dt = dt
Return dTypes
Catch ex As Exception
Throw New Exception("SQLSelectCommand: " &
ex.ToString() & " SQL: " & sqlCom.CommandText.ToString)
Finally
If Not sqlDR Is Nothing Then
If sqlDR.IsClosed = False Then
sqlDR.Close()
End If
End If
If sqlCon.State = ConnectionState.Open Or
sqlCon.State = ConnectionState.Connecting Or sqlCon.State =
ConnectionState.Executing Or sqlCon.State = ConnectionState.Fetching
Then
sqlCon.Close()
End If
sqlCon.Dispose()
End Try
End Get
End Property
PLEASE NOTE: The SQL Connection is initialized in the constructor for
the class, so the connection can never be nothing or according to me
un-initialized.
There are also a bunch of other properties in the same class that will
use the same connection.

Public Sub New()
sqlCon = New
SqlConnection(ConfigurationSettings.AppSettings("d bConnectionString"))
End Sub

Public Sub New(ByVal SqlConnectionString As String)
sqlCon = New SqlConnection(SqlConnectionString)
End Sub

Public Sub New(ByVal SqlConnectionString As String, ByVal
DefaultTableSize As Integer)
sqlCon = New SqlConnection(SqlConnectionString)
Dim ns(DefaultTableSize) As String
defSqlStrings = ns
sTbls = New sTables(DefaultTableSize, Me)
End Sub

Nov 21 '05 #1
3 1455
Rykie,

Why do you make it yourself so difficult and do not create the object
completly new everytime you need it (you can put the connectionstring
somewhere global). OOP is build on this principle so don't assume that
creating of an object takes much time.

You than need only every time in your methods
sub/function whatever
dim conn as new sqlconnection(connectionstring)
conn.open
'do what you want to do
conn.close
end sub/function

Than you normally cannot have any pooling or whatever problem.

I hope this helps,

Cor
Nov 21 '05 #2
Cor,

Thank you, I have taken that approach now, and I will see how it goes
and post back here my findings.

Rykie

Nov 21 '05 #3
Thank you, that fixed it.

Nov 21 '05 #4

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

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
4
by: VB Programmer | last post by:
I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
3
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
1
by: Steve | last post by:
Hi, I currently display all the data on a website using tableadaptors and objectdatasources. Would it be significantly faster if I was to display the data by writing the code for a datareader...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.