473,831 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(By Val arg_SQL As String, Optional ByVal
arg_isStoredPro c As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connecti on = loc_DBconn

If arg_isStoredPro c Then

m_oCmd.CommandT ype = CommandType.Sto redProcedure

Else

m_oCmd.CommandT ype = CommandType.Tex t ''default

End If

m_oCmd.CommandT ext = arg_SQL

Return m_oCmd.ExecuteR eader

Catch eX As System.Exceptio n

Throw

End Try

End Function

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

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("s elect 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 1263
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(By Val arg_SQL As String, Optional ByVal
arg_isStoredPro c As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connecti on = loc_DBconn

If arg_isStoredPro c Then

m_oCmd.CommandT ype = CommandType.Sto redProcedure

Else

m_oCmd.CommandT ype = CommandType.Tex t ''default

End If

m_oCmd.CommandT ext = arg_SQL

Return m_oCmd.ExecuteR eader

Catch eX As System.Exceptio n

Throw

End Try

End Function

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

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("s elect 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*****@singne t.com.sg> wrote in message
news:eT******** ********@tk2msf tngp13.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(By Val arg_SQL As String, Optional ByVal
arg_isStoredPro c As Boolean = False) As OdbcDataReader

Try

Dim m_oCmd As New OdbcCommand

m_oCmd.Connecti on = loc_DBconn

If arg_isStoredPro c Then

m_oCmd.CommandT ype = CommandType.Sto redProcedure

Else

m_oCmd.CommandT ype = CommandType.Tex t ''default

End If

m_oCmd.CommandT ext = arg_SQL

Return m_oCmd.ExecuteR eader

Catch eX As System.Exceptio n

Throw

End Try

End Function

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

Public Function version() As String

Dim retval As String

Try

Dim m_oReader As OdbcDataReader

m_oReader = executeSQL_Q("s elect 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
5766
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
3060
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 update another table with that information. However, I'm getting the following error message. There is already an open DataReader associated with this Connection which must be closed first. Description: An unhandled exception occurred during the...
0
1162
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" runat="server" /> <asp:dropdownlist id="ddlEndTimeHour" runat="server" /> <asp:dropdownlist id="ddlStartTimeMinute" runat="server" />
1
3093
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 string get_array(string sql) { //create db connection & open
2
3401
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 4 2
2
6975
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
2
2202
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 an integer (32 possible values) The following methods (translated from VB6) should help with this?: Public Sub BitSet(ByVal iValue As Integer, ByVal Bitnum As Integer, ByVal State As Boolean) Try
10
6113
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 code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed first' Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
1
3966
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;" + "Persist Security Info=yes;" + "UserId=chat;PWD=123;Port=3306;Database=im_chat;");
3
8542
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 following questions: 1. Can DataReader be bound DIRECTLY to DataGridView? 2. If DataReader cannot be bound directly to DataGridView, how can I load a DataReader into a DataTable and bind the DataTable to DataGridView? Can the DataTable be a...
0
9793
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10777
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10494
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10534
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9317
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7748
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4417
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

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.