473,503 Members | 1,655 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_KeyPress(ByVal sender As Object, ByVal e A
System.Windows.Forms.KeyPressEventArgs) Handles txtItemCode.KeyPress
Dim db As New OleDbCommand _
("SELECT * FROM [Item Master]" & "WHERE ItemCode ='"
txtItemCode.Text & "'", dc)
dc.Open()

dr = db.ExecuteReader
Dim total As Single

While dr.Read()
txtDescription.Text = dr.Item(2)
txtUnitPrice.Text = 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(ByVal sender As System.Object, ByVal e A
System.EventArgs) Handles btnOk.Click
If txtItemCode.Text = "" Then
MessageBox.Show("Please fill in Item Code!", "Sale"
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtItemCode.Focus()
Exit Sub
End If

Try
Dim strSQL As String

strSQL = "INSERT INTO SaleByItem " & _
"(ItemCode, Description,Qty,UnitPrice,Total)VALUE
" & _
"('" & txtItemCode.Text & "'," & _
"'" & txtDescription.Text & "','"
NumericUpDown1.Text & "', " & _
"'" & txtUnitPrice.Text & "','" & txtTotal.Text
"')"
dc.Open()
Dim cmd As New OleDbCommand(strSQL, dc)
cmd.ExecuteNonQuery()
Call listview()
dc.Close()

Exit Sub

Catch
MessageBox.Show("Error occured when trying to save data!",
"Sale", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtItemCode.Focus()
Exit Sub
End Try
txtItemCode.Focus()
txtItemCode.Clear()
txtDescription.Clear()
NumericUpDown1.Text = 1
txtUnitPrice.Clear()
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 1828
Your KeyPress handler does not close the connection. If tehre is a row
returned, it exits the function before closing the connection.

"lea" <le********@no-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_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtItemCode.KeyPress
Dim db As New OleDbCommand _
("SELECT * FROM [Item Master]" & "WHERE ItemCode ='" &
txtItemCode.Text & "'", dc)
dc.Open()

dr = db.ExecuteReader
Dim total As Single

While dr.Read()
txtDescription.Text = dr.Item(2)
txtUnitPrice.Text = 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOk.Click
If txtItemCode.Text = "" Then
MessageBox.Show("Please fill in Item Code!", "Sale",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtItemCode.Focus()
Exit Sub
End If

Try
Dim strSQL As String

strSQL = "INSERT INTO SaleByItem " & _
"(ItemCode, Description,Qty,UnitPrice,Total)VALUES
" & _
"('" & txtItemCode.Text & "'," & _
"'" & txtDescription.Text & "','" &
NumericUpDown1.Text & "', " & _
"'" & txtUnitPrice.Text & "','" & txtTotal.Text &
"')"
dc.Open()
Dim cmd As New OleDbCommand(strSQL, dc)
cmd.ExecuteNonQuery()
Call listview()
dc.Close()

Exit Sub

Catch
MessageBox.Show("Error occured when trying to save data!",
"Sale", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtItemCode.Focus()
Exit Sub
End Try
txtItemCode.Focus()
txtItemCode.Clear()
txtDescription.Clear()
NumericUpDown1.Text = 1
txtUnitPrice.Clear()
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.........my 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
6456
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...
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...
14
4589
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 _...
8
14542
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. ...
16
2843
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...
20
3242
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...
9
4749
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...
2
1353
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...
0
7202
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
7086
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
7280
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,...
0
7330
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...
1
5014
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
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
0
380
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...

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.