473,387 Members | 1,760 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,387 software developers and data experts.

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 1821
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.