473,651 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connection State Is Already Open

lea

i create few textboxes, if i enter data in txtItemCode, the rest txtbo
will display data that read from database. show below.
=============== =============== =============== ============= Private Su
txtItemCode_Key Press(ByVal sender As Object, ByVal e A
System.Windows. Forms.KeyPressE ventArgs) Handles txtItemCode.Key Press
Dim db As New OleDbCommand _
("SELECT * FROM [Item Master]" & "WHERE ItemCode ='"
txtItemCode.Tex t & "'", dc)
dc.Open()

dr = db.ExecuteReade r
Dim total As Single

While dr.Read()
txtDescription. Text = dr.Item(2)
txtUnitPrice.Te xt = dr.Item(4)
Exit Sub

End While
dc.Close()

End Sub
=============== =============== =============== =============
then i got a btnOk to enter all the data into listview.
=============== =============== =============== =============
Private Sub btnOk_Click(ByV al sender As System.Object, ByVal e A
System.EventArg s) Handles btnOk.Click
If txtItemCode.Tex t = "" Then
MessageBox.Show ("Please fill in Item Code!", "Sale"
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
txtItemCode.Foc us()
Exit Sub
End If

Try
Dim strSQL As String

strSQL = "INSERT INTO SaleByItem " & _
"(ItemCode, Description,Qty ,UnitPrice,Tota l)VALUE
" & _
"('" & txtItemCode.Tex t & "'," & _
"'" & txtDescription. Text & "','"
NumericUpDown1. Text & "', " & _
"'" & txtUnitPrice.Te xt & "','" & txtTotal.Text
"')"
dc.Open()
Dim cmd As New OleDbCommand(st rSQL, dc)
cmd.ExecuteNonQ uery()
Call listview()
dc.Close()

Exit Sub

Catch
MessageBox.Show ("Error occured when trying to save data!",
"Sale", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
txtItemCode.Foc us()
Exit Sub
End Try
txtItemCode.Foc us()
txtItemCode.Cle ar()
txtDescription. Clear()
NumericUpDown1. Text = 1
txtUnitPrice.Cl ear()
txtTotal.Clear( )
End Sub
=============== =============== =============== =============
but when i first click on btnOk, the msgbox in blue color font abov
prompt out,if i click second time on the btnOK , then it work. th
third time i click on btnOk,the error msg prompt out and state that m
CONNECTION STATE IS ALREADY OPEN. and highlighted the code whic
occured erro(YELLOW COLOR) and immediately close my program. the cod
in red color are cant work also, but i remember the first time i try
dont have this kind of error.can someone pls tell me where im wrong? :

--
le
-----------------------------------------------------------------------
lea's Profile: http://www.msusenet.com/member.php?userid=156
View this thread: http://www.msusenet.com/t-187051032

Jul 21 '05 #1
2 1851
Your KeyPress handler does not close the connection. If tehre is a row
returned, it exits the function before closing the connection.

"lea" <le********@n o-mx.msusenet.com > wrote in message
news:le******** @no-mx.msusenet.com ...

i create few textboxes, if i enter data in txtItemCode, the rest txtbox
will display data that read from database. show below.
=============== =============== =============== ============= Private Sub
txtItemCode_Key Press(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles txtItemCode.Key Press
Dim db As New OleDbCommand _
("SELECT * FROM [Item Master]" & "WHERE ItemCode ='" &
txtItemCode.Tex t & "'", dc)
dc.Open()

dr = db.ExecuteReade r
Dim total As Single

While dr.Read()
txtDescription. Text = dr.Item(2)
txtUnitPrice.Te xt = dr.Item(4)
Exit Sub

End While
dc.Close()

End Sub
=============== =============== =============== =============
then i got a btnOk to enter all the data into listview.
=============== =============== =============== =============
Private Sub btnOk_Click(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles btnOk.Click
If txtItemCode.Tex t = "" Then
MessageBox.Show ("Please fill in Item Code!", "Sale",
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
txtItemCode.Foc us()
Exit Sub
End If

Try
Dim strSQL As String

strSQL = "INSERT INTO SaleByItem " & _
"(ItemCode, Description,Qty ,UnitPrice,Tota l)VALUES
" & _
"('" & txtItemCode.Tex t & "'," & _
"'" & txtDescription. Text & "','" &
NumericUpDown1. Text & "', " & _
"'" & txtUnitPrice.Te xt & "','" & txtTotal.Text &
"')"
dc.Open()
Dim cmd As New OleDbCommand(st rSQL, dc)
cmd.ExecuteNonQ uery()
Call listview()
dc.Close()

Exit Sub

Catch
MessageBox.Show ("Error occured when trying to save data!",
"Sale", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
txtItemCode.Foc us()
Exit Sub
End Try
txtItemCode.Foc us()
txtItemCode.Cle ar()
txtDescription. Clear()
NumericUpDown1. Text = 1
txtUnitPrice.Cl ear()
txtTotal.Clear( )
End Sub
=============== =============== =============== =============
but when i first click on btnOk, the msgbox in blue color font above
prompt out,if i click second time on the btnOK , then it work. the
third time i click on btnOk,the error msg prompt out and state that my
CONNECTION STATE IS ALREADY OPEN. and highlighted the code which
occured erro(YELLOW COLOR) and immediately close my program. the code
in red color are cant work also, but i remember the first time i try,
dont have this kind of error.can someone pls tell me where im wrong? :(
--
lea
------------------------------------------------------------------------
lea's Profile: http://www.msusenet.com/member.php?userid=1569
View this thread: http://www.msusenet.com/t-1870510325

Jul 21 '05 #2
lea

oh yea! that's my mistake to put in the Exit Sub before the dc.close()
Thanks your help, what a simple error but i debug for whol
night.........m y choice is right to post my problem here! Really thank
again! :

--
le
-----------------------------------------------------------------------
lea's Profile: http://www.msusenet.com/member.php?userid=156
View this thread: http://www.msusenet.com/t-187051032

Jul 21 '05 #3

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

Similar topics

1
6476
by: ST | last post by:
This is my other error when I click on Immunoflourescence. I believe this is related to the other error I just posted (Input string was not in a correct format.) Please let me know if you have any suggestions! Thanks! The connection is already Open (state=Open). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where...
2
281
by: lea | last post by:
i create few textboxes, if i enter data in txtItemCode, the rest txtbo will display data that read from database. show below. ========================================================== Private Su txtItemCode_KeyPress(ByVal sender As Object, ByVal e A System.Windows.Forms.KeyPressEventArgs) Handles txtItemCode.KeyPress Dim db As New OleDbCommand _ ("SELECT * FROM " & "WHERE ItemCode ='" txtItemCode.Text & "'", dc) dc.Open()
14
4603
by: martin1 | last post by:
All, I want to check wether db connection/open or not, if not it will post db connection error to user in message box The code is like: Dim objConnection As New SqlConnection _ ("Server=server1; database=db1;user id=user1;password=whatever") Dim objDataAdapter As New SqlDataAdapter() Dim objDataSet
8
14553
by: Imran Aziz | last post by:
Hello All, Like in C++ I tried to use constructor to open a database connection and distructor to close the database connection, it now turns out that one cannot create distrutors in C# classes. Here is my code public class DBLayer {
16
2862
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with a worker thread that would execute the db commands. The pool was fixed and all the connections were created when the db access class was instantiated. The connections remained opened during the whole execution. If a connection was not available...
20
3269
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how many connection has been used ? 2. If the maximum pool size has been reached, what happens when I call the method Open to open the connection ? Will I get an error ? MSDN says the request is queued, but will I get an error in the open method ? ...
9
4756
by: fniles | last post by:
I am using VB.NET 2003 and SQL 2005. To use connection pooling and avoid the error "There is already an open DataReader associated with this Connection which must be closed first." , I understand that I want to release/close connections in a timely fashion. What I do is I declare the SqlClient.SqlConnection variable as a local variable inside my subroutine (instead of a global variable). Right before I Fill the dataset I open the...
2
1356
by: pmclinn | last post by:
When I'm connecting to Oracle or SQL I always wonder what is the best way to close/dispose of a connection. I've been toying around with creating a public shared class that has a connection string and then calling on the shared class when needed. (See raw examples below.) If two of my users open up two different pages at the same time will this still work. And if so should I pre-test to see if the connection state is already open using...
0
8357
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
8700
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
8465
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
8581
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
7298
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...
0
4144
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1
1910
muto222
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.