473,397 Members | 2,033 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,397 software developers and data experts.

there is already an open DataReader associated with this Command which must be closed

3
i am using vb asp.net

error: there is already an open DataReader associated with this Command which must be closed first.

working on this issue for couple weeks now and i have no idea how to fix this. any help will be helpful

i am using 2 reader on page load to display some information, but each of them i am closing it.

after that i am using 3rd reader on button click, which is giving me this error

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         If Not IsPostBack Then
  3.             ...
  4.             Dim reader As SqlDataReader = cmd1.ExecuteReader()
  5.             While reader.Read()
  6.                 ...
  7.             End While
  8.             reader.Close()
  9.             reader.Dispose()
  10.  
  11.             ...
  12.             Dim reader2 As SqlDataReader = cmd1.ExecuteReader()
  13.             dt.Load(reader2)
  14.             reader2.Close()
  15.             reader2.Dispose()
  16.  
  17.             BagRepeater.DataSource = dt
  18.             BagRepeater.DataBind()
  19.         End If
  20. End Sub
  21.  
  22. Public Sub CHECKOUTLB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CHECKOUTLB.Click
  23.  Dim Order_ID As Long
  24.             Dim InsertOrderTBSelect As String = "Select @@Identity"
  25.             Dim InsertOrderTB = "INSERT INTO [ORDER_TB] ... "
  26.             Dim cmd1 = New SqlCommand(InsertOrderTB, sqlConn)
  27.             cmd1.ExecuteNonQuery()
  28.             cmd1.CommandText = InsertOrderTBSelect
  29.             Order_ID = cmd1.ExecuteScalar()
  30.  
  31.             Dim reader3 As SqlDataReader = cmd3.ExecuteReader()
  32.             While reader3.Read()
  33.                 Dim InsertOrderDetailTB = "INSERT INTO [ORDER_Detail_TB] ... "
  34.                 Dim cmd4 = New SqlCommand(InsertOrderDetailTB, sqlConn)
  35.                 cmd4.Parameters.AddWithValue("@Order_ID", Order_ID)
  36.                 cmd4.Parameters.AddWithValue("@Product_ID", reader3("Product_ID"))
  37.                 cmd4.Parameters.AddWithValue("@QTY", reader3("QTY"))
  38.                 cmd4.Parameters.AddWithValue("@Color", reader3("Color"))
  39.                 cmd4.ExecuteNonQuery()
  40.             End While
  41.             reader3.Close()
  42.  
Feb 21 '18 #1
1 3299
Frinavale
9,735 Expert Mod 8TB
I believe your problem is that you are using the same command cmd1 for readers.

Consider making use of the using statement to properly close and dispose of unmanaged code supporting the .NET objects used for getting and setting data from/to the database.



For example:
Expand|Select|Wrap|Line Numbers
  1. If Not IsPostBack Then
  2.   ' ...
  3.   Using connection As New New SqlConnection(connectionString)
  4.     Using cmd1 as New SqlCommand("Select * From Table", connection)
  5.       Using reader As SqlDataReader = cmd1.ExecuteReader()
  6.         If reader.HasRows Then
  7.           While reader.Read()
  8.             '...
  9.           End While
  10.         End If
  11.       End Using
  12.     End Using
  13.   End Using
  14.  
  15.  ' ...
  16.   Using connection As New New SqlConnection(connectionString)
  17.     Using cmd2 as New SqlCommand("Select * From AnotherTable", connection)
  18.       Using reader2 As SqlDataReader = cmd2.ExecuteReader()
  19.         If reader2.HasRows Then
  20.           While reader2.Read()
  21.             '...
  22.           End While
  23.         End If
  24.       End Using
  25.     End Using
  26.   End Using
  27.  
  28.   BagRepeater.DataSource = dt
  29.   BagRepeater.DataBind()
  30. End If
  31.  
Feb 21 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
10
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...
13
by: Bart | last post by:
Hi, i get the error: "There is already an open DataReader associated with this Command which must be closed first" Thanks Bart ----------------------------------------- Imports...
3
by: BLUE | last post by:
I've a TransactionScope in which I select some data and I want to do some queries for each record retrieved with my select. I'm using a DataReader and for each record I do factory.CreateCommand()...
3
by: yogeshraj | last post by:
I am using visual studio 2012 with SqlServer 2008. while developing a website, i used datareader when i wanted to use the datareader again for reading another table data from the same database, i...
0
by: kartheekPeri | last post by:
how to avoid the already open datareader associated command It happens while running data reader more than two times in three tier architecture.
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...
0
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,...
0
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...

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.