473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
I had created a shopping cart website using visual studio 2005 web developer
I got this error when i doing update the shopping cart, can anyone please help me solve this problem thanks very much
Expand|Select|Wrap|Line Numbers
  1.   Dim cn As New SqlClient.SqlConnection()
  2.             cn.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString1").ConnectionString
  3.  
  4.             Dim ProductId As String
  5.             ProductId = Session("PI")
  6.             Dim userID As String
  7.             userID = User.Identity.Name()
  8.  
  9.             Dim sql As String
  10.             sql = "SELECT * FROM [Order] WHERE [UserId] = @userID AND [ProductId] = @ProductId "
  11.  
  12.             Dim cmd1 As New SqlCommand(sql, cn)
  13.             cmd1.Parameters.AddWithValue("UserId", userID)
  14.             cmd1.Parameters.AddWithValue("ProductId", ProductId)
  15.  
  16.             cn.Open()
  17.             Dim dr As SqlDataReader
  18.             dr = cmd1.ExecuteReader
  19.             dr.Read()
  20.  
  21.             If dr.Read() Then
  22.  
  23.                 sql = "UPDATE [Order] SET Quantity=@Quantity , SubTotal=@SubTotal" & _
  24.                 "WHERE ProductId=@ProductId" & _
  25.                 "AND UserId=@UserId"
  26.  
  27.                 Dim cmd2 As New SqlCommand(sql, cn)
  28.  
  29.                 cmd2.Parameters.AddWithValue("@Quantity", Session("Quantity"))
  30.                 cmd2.Parameters.AddWithValue("@ProductId", ProductId)
  31.                 cmd2.Parameters.AddWithValue("UserId", userID)
  32.                 cmd2.Parameters.AddWithValue("@SubTotal", Session("Price"))
  33.                 cmd2.ExecuteNonQuery()
  34.  
  35.  
  36.             Else
  37.  
  38.                 sql = "INSERT INTO Order " & _
  39.                       "(UserId, SubTotal, Price, OrderDate, ProductId,Quantity) " & _
  40.                       "VALUES (@UserId, @SubTotal, @Price,@OrderDate, @ProductId,@Quantity)"
  41.                 Dim cmd3 As New SqlCommand(sql, cn)
  42.                 cmd3.Parameters.AddWithValue("@OrderDate", FormatDateTime(Today, DateFormat.LongDate))
  43.                 cmd3.Parameters.AddWithValue("@Price", Session("Price"))
  44.                 cmd3.Parameters.AddWithValue("@SubTotal", Session("Price"))
  45.                 cmd3.Parameters.AddWithValue("@Quantity", Session("Quantity"))
  46.                 cmd3.Parameters.AddWithValue("@ProductId", ProductId)
  47.                 cmd3.Parameters.AddWithValue("UserId", userID)
  48.                 cmd3.ExecuteNonQuery()
  49.  
  50.  
  51.             End If
  52.             cn.Close()
  53.             cmd1.Dispose()
  54.             cn.Dispose()
  55.  
  56.         End If
Jan 8 '08 #1
1 1659
jhardman
3,406 Recognized Expert Specialist
which line gives the error?

By the way, this is ASP.NET, not "classic" ASP, right?

Jared
Jan 8 '08 #2

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

Similar topics

2
2854
by: Grant | last post by:
Hi, I keep getting the following error message: InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first. I have read up about it and realise that you can only have one DataReader open on a Connection at a time, but don't know how to rewrite my code to work properly.
20
7189
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 get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each...
10
6091
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 code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed...
13
3401
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 System.Data.sqlclient
3
13189
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() and then I execute the command, but I get the following exception message: " There is already an open DataReader associated with this Command which...
11
3148
by: =?Utf-8?B?QXNhZg==?= | last post by:
Hello, I have two Table Adapters that I am storing in a Cache using .NET 2.0 Web Service and the Database is SQL Server on Windows 2003 IIS6. Each of the two table adapters has its own Connection string. The reason that table adapters are in cache is because I would like to maintain the connection string that will not be closed for any...
0
7693
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...
0
7605
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...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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...
1
7665
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...
0
5217
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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
0
933
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...

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.