473,651 Members | 2,793 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 1850
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
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
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...
0
8579
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...
1
6158
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
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.