473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

weird problem with datareader

Cc
hi,
while I making my own class library using odbc.net I encounter strange
problem
in this class
Public Sub New()

' some code doing openning connection

end sub

Protected Overrides Sub Finalize()

'somecode doing closing connection

MyBase.Finalize()

End Sub

the idea in here is to save trouble from openning and closing connection .
in this class I have some function

Private Function executeSQL_Q(ByVal arg_SQL As String, Optional ByVal
arg_isStoredProc As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connection = loc_DBconn

If arg_isStoredProc Then

m_oCmd.CommandType = CommandType.StoredProcedure

Else

m_oCmd.CommandType = CommandType.Text ''default

End If

m_oCmd.CommandText = arg_SQL

Return m_oCmd.ExecuteReader

Catch eX As System.Exception

Throw

End Try

End Function

----------------------------------------------------------------------------
--------------------------------------------

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("select VERSION();")

m_oReader.Read()

retval = m_oReader.Item("VERSION()")

m_oReader.Close()

Catch eX As Exception

Throw

End Try

Return retval

End Function

as you see this is only a very simple function. when calling this class
function once and exist program it work fine, the problem only occur
whenever I call this function more than once and exit the program that
reference this class will get unhandle error (I can't get the error even
though I use try and catch exception).

after a while I finaly found where this error occur. to solve this I must
manual close the connection rather than let the finalize procedure do it for
me

Is there a way to let it the close connection at finalize rather than I have
to manualy close without having this error ?




Nov 20 '05 #1
2 1242
On Sat, 22 May 2004 04:53:55 +0800, Cc wrote:
hi,
while I making my own class library using odbc.net I encounter strange
problem
in this class
Public Sub New()

' some code doing openning connection

end sub

Protected Overrides Sub Finalize()

'somecode doing closing connection

MyBase.Finalize()

End Sub

the idea in here is to save trouble from openning and closing connection .
in this class I have some function

Private Function executeSQL_Q(ByVal arg_SQL As String, Optional ByVal
arg_isStoredProc As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connection = loc_DBconn

If arg_isStoredProc Then

m_oCmd.CommandType = CommandType.StoredProcedure

Else

m_oCmd.CommandType = CommandType.Text ''default

End If

m_oCmd.CommandText = arg_SQL

Return m_oCmd.ExecuteReader

Catch eX As System.Exception

Throw

End Try

End Function

----------------------------------------------------------------------------
--------------------------------------------

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("select VERSION();")

m_oReader.Read()

retval = m_oReader.Item("VERSION()")

m_oReader.Close()

Catch eX As Exception

Throw

End Try

Return retval

End Function

as you see this is only a very simple function. when calling this class
function once and exist program it work fine, the problem only occur
whenever I call this function more than once and exit the program that
reference this class will get unhandle error (I can't get the error even
though I use try and catch exception).

after a while I finaly found where this error occur. to solve this I must
manual close the connection rather than let the finalize procedure do it for
me

Is there a way to let it the close connection at finalize rather than I have
to manualy close without having this error ?


If I'm not mistaken, you cannot be assured when code in the Finalize method
will be called. It may never be called at all. That's why your class
should implement IDisposable. When you are finished with the class, call
its dispose method. In that method you should close the connection.

--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #2
Do *NOT* close/dispose a sqlclient object (connection, for example) in a
finalize method (see link).
Open and close the connection as needed. SQL connection pooling will ensure
you get good performance, so you don't need to keep the connection open the
whole time (the unmanaged connection in the pool will actually stay open
until it times out).

http://support.microsoft.com/default...b;en-us;827366

-Rob Teixeira [MVP]

"Cc" <ko*****@singnet.com.sg> wrote in message
news:eT****************@tk2msftngp13.phx.gbl...
hi,
while I making my own class library using odbc.net I encounter strange
problem
in this class
Public Sub New()

' some code doing openning connection

end sub

Protected Overrides Sub Finalize()

'somecode doing closing connection

MyBase.Finalize()

End Sub

the idea in here is to save trouble from openning and closing connection .
in this class I have some function

Private Function executeSQL_Q(ByVal arg_SQL As String, Optional ByVal
arg_isStoredProc As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connection = loc_DBconn

If arg_isStoredProc Then

m_oCmd.CommandType = CommandType.StoredProcedure

Else

m_oCmd.CommandType = CommandType.Text ''default

End If

m_oCmd.CommandText = arg_SQL

Return m_oCmd.ExecuteReader

Catch eX As System.Exception

Throw

End Try

End Function

-------------------------------------------------------------------------- -- --------------------------------------------

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("select VERSION();")

m_oReader.Read()

retval = m_oReader.Item("VERSION()")

m_oReader.Close()

Catch eX As Exception

Throw

End Try

Return retval

End Function

as you see this is only a very simple function. when calling this class
function once and exist program it work fine, the problem only occur
whenever I call this function more than once and exit the program that
reference this class will get unhandle error (I can't get the error even
though I use try and catch exception).

after a while I finaly found where this error occur. to solve this I must
manual close the connection rather than let the finalize procedure do it for me

Is there a way to let it the close connection at finalize rather than I have to manualy close without having this error ?



Nov 20 '05 #3

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

Similar topics

5
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
4
by: Maziar Aflatoun | last post by:
Hi everyone, I am working on the 'Delete' section of my program. What I need to do is query my database and for every ID that it finds I want to remove a file+ID.jpg from my file folder and...
0
by: dhnriverside | last post by:
Hi peeps OK, I'm getting a weird error trying to select a DDL item based on what's stored in the database. I have 4 DDLs on my aspx page... <asp:dropdownlist id="ddlStartTimeHour"...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
2
by: orencs | last post by:
Hi, I am using Datareader and stored procedure in C# ADO.NET. When I am running the stored procedure in the SQL Query Analyzer and recieve two rows (as I hace expected) col1 col2 0 1 0 ...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: Harry Strybos | last post by:
Hi All I have a form with about 20 check boxes and I want to persist their state to a single integer. That is to say, I want to set their Checked property to a particular bit value contained in...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
1
by: jinxy | last post by:
Hello, this is my code in visual c#: namespace im_client { public partial class Form2 : Form { MySqlConnection conDatabase = new MySqlConnection("Data Source=86.123.220.28;" +...
3
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.