Results 1 to 32 of 32

Thread: [RESOLVED] [2008] Returning multiple values from an Access Database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Resolved [RESOLVED] [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    what is the purpose of using commandBehavior.SingleRow?
    You will then return only one row from your db even if they are more than one.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    and in your db you have multiple rows for sure?
    have tried executing your query directly in your db to see what the result is?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    instead of
    vb Code:
    1. if reader.read
    use
    vb Code:
    1. if reader.hasrows()
    2.   while reader.read()
    3.     'your code here
    4.   end while
    5.  
    6.   reader.close
    7. end if

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.

  8. #8
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    I noticed that you are over writing the value of itemGroup.ItemCode and itemgroup.ItemGroupID every time.
    if I'm right those values should always correspond to the last item read in the reader.
    may be you meat to work in some kind of collection where you store all the records.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    I am not sure how to write a collection for this...

    ???

  10. #10
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    vb Code:
    1. Public Shared Function ReturnItems(ByVal sGroup As String) As list(of cItemGroup)
    2.         Dim itemgroup As New cItemGroup
    3.         dim myList as List(of cItemGroup) = new List(of cItemGroup)
    4.  
    5.         Dim connection As OleDbConnection = DBconnect.GetConnection3
    6.         Dim sSQL As String = "SELECT Itemcode, M_ItemGroupID " & _
    7.         "FROM Cust WHERE M_ItemGroupID = '" & sGroup & "'"
    8.  
    9.         Dim runSQL As New OleDbCommand(sSQL, connection)
    10.         Try
    11.             connection.Open()
    12.             Dim reader As OleDbDataReader = runSQL.ExecuteReader(CommandBehavior.SingleRow)
    13.             while reader.Read
    14.                 itemgroup.Itemcode = reader("Itemcode").ToString
    15.                 'itemgroup.ItemID = reader("M_ItemID").ToString
    16.                 itemgroup.ItemGroupID = reader("M_ItemGroupID").ToString
    17.                 myList.add(itemGroup)
    18.             end while
    19.             reader.Close()
    20.         Catch ex As OleDbException
    21.             itemgroup = Nothing
    22.         Finally
    23.             connection.Close()
    24.         End Try
    25.         Return myList
    26.     End Function

    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         dim myList as List(of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text)
    3.  
    4.         for each cig as sItemGroup in myList
    5.             messageBox.show(cig.ItemCode & " " & cig.ItemGroupID)
    6.         next cig
    7.     End Sub

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  12. #12
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    if you want to view all the item at once, you can put them in a listbox or listview(more likely because yo have at list two columns in your query). it is easier that having to maintain a bunch of labels

    edited: Listview link
    Last edited by talkro; May 16th, 2008 at 09:54 AM.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  14. #14
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] Returning multiple values from an Access Database

    I've had this problem before too. The reason is that reader("M_ItemGroupID").ToString is not a string, it's a pointer to an object reference and the item in the listbox is not a string, it's an object. When the reader moves onto the next row, the pointer is then pointing to a reference with a different value, rather than what was there during the previous loop, so the object that gets displayed in the listbox is now something completely different than what you wanted to put in there.

    Why it works this way, I don't know - it strikes me as somewhat silly behaviour that likely has a boring technical rationale behind it which only four people in the world care about.

    To fix it, change your code to this:

    Code:
    while reader.Read      
    
        dim sCode as String =  reader("Itemcode").ToString 
        dim sGroupID as String = reader("M_ItemGroupID").ToString 
        itemgroup.Itemcode = sCode
        itemgroup.ItemGroupID = sGroupID             
        myList.add(itemGroup)            
    end while
    This creates new strings each time through the loop and adds those values to the listbox, so each item is referencing something different and they don't change as the reader moves onto the next row.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  17. #17
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    can you post the code you are using at this moment?

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Unhappy Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  20. #20
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    The error you get about Index out or range is because in your query you are only getting one column from the database. The number of items you read from the sqlDataReader must be consistent with the number of columns you are retrieving from the database.

  21. #21
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    replace this code
    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.    Dim myList As List(Of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text)
    3.    For Each item As cItemGroup In myList
    4.       ListBox1.Text = item.Itemcode
    5.    Next item
    6. End Sub

    by
    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.    Dim myList As List(Of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text)
    3.    me.listbox1.Items.AddRange(myList)
    4. End Sub

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:47 AM.

  24. #24
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    my mistake, the AddRange method takes an array as parameter, so what you have to do is convert the list of to a convetional array

    vb Code:
    1. Me.ListBox1.Items.AddRange(myList.ToArray())

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  26. #26
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    I tried this piec of code which is similar to yours except for the database and it works fine

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim input() As String = New String() {"1", "2", "3", "4", "5", "6", "7", "8"}
    3.         Dim mylist As List(Of String) = New List(Of String)
    4.  
    5.         'mylist.AddRange(input)
    6.         For Each str As String In input
    7.             mylist.Add(str)
    8.         Next str
    9.  
    10.         Me.ListBox1.Items.AddRange(mylist.ToArray)
    11. End Sub

    Use
    vb Code:
    1. Dim sCode As String = reader.GetString(0)
    instead of
    vb Code:
    1. Dim sCode As String = reader("Itemcode").ToString

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.

  28. #28
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    the problem is comming from the fact that the list that you return from the function that reads the table is collection of your object. When you use the 'ToArray' method, it accesses each object in the list and then gets the description ('Stockist_Price_Book.cItemGroup' ...) of the given object as defined in the 'ToString' function by default.

    perhaps you should ignore post #26, but then overwrite the ToString method in you class 'cItemGroup' and make it return the 'itemcode'

    vb.net Code:
    1. public class cItemGroup
    2.   Public Overrides Function ToString() As String
    3.         return me.m_itemCode
    4.     End Function
    5. end class

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:37 AM.

  30. #30
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] Returning multiple values from an Access Database

    debug the Returnitem function and check what are the values added to 'myList'. what are the item in the list?

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Re: [2008] Returning multiple values from an Access Database

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:37 AM.

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    122

    Smile Problem solved

    ....
    Last edited by robyn.berry; Jun 12th, 2008 at 03:37 AM.

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