Results 1 to 4 of 4

Thread: Database Search Question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Database Search Question

    I've got a code using SQLClient that loops though all users in a Users table inside each loop i want to preform another search in the UserOrders table, thus doing a count of all orders.

    I get this error

    HTML Code:
    There is already an open DataReader associated with this Command which must be closed first.
    Here is my code

    vb.net Code:
    1. UserLists.Items.Clear()
    2.             Dim connection As New SqlClient.SqlConnection(My.Settings.ConnectString)
    3.                 Dim command As New SqlClient.SqlCommand("SELECT * FROM Users ORDER BY PersonName", connection)
    4.             connection.Open()
    5.  
    6.             Dim reader As SqlClient.SqlDataReader = command.ExecuteReader
    7.             Dim Ops As String = ""
    8.             While reader.Read
    9.  
    10.                 Dim ID As Integer = reader("ID")
    11.                 Dim command2 As New SqlClient.SqlCommand("SELECT Count(ID) FROM UserOrders WHERE UserID = @ID", connection)
    12.                 command2.Parameters.AddWithValue("@ID", ID)
    13.                 Dim count = command2.ExecuteScalar
    14.  
    15.  
    16.                 Dim listitem As New ListViewItem
    17.                 listitem.Text = reader("Username")
    18.                 listitem.SubItems.Add(reader("PersonName"))
    19.                 listitem.SubItems.Add(count)
    20.                 listitem.Tag = reader("ID")
    21.                 UserLists.Items.Add(listitem)
    22.  
    23.             End While
    24.             connection.Close()

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Database Search Question

    Another way of doing it would be to loop through each item in my listview but my questions then are.

    For Each item in UsersListView

    Next

    Question, how do i get the value from the 3rd or 4th coloumn and how do i update a row?

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Database Search Question

    A better way to do this write a SQL statment that already counts the orders for each Person

    Roughly:

    Code:
    SELECT 
    Prs.Id
    , Prs.Name
    , Prs.Surname
    , Prs.Gender
    , Count(Ord.OrderID) as NumberOfOrders
    FROM
     Person Prs
    INNER JOIN 
    Orders Ord
    ON 
    Ord.PersonId = Prs.Id
    GROUP BY
    Prs.Id
    , Prs.Name
    , Prs.Surname
    , Prs.Gender
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  4. #4
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Database Search Question

    You get that error because you have this command
    Code:
    command2.ExecuteScalar
    executing against the same connection as
    Code:
    command.ExecuteReader
    The join that Dnereb gives you would solve your problem.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width