|
-
May 16th, 2008, 06:24 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.
-
May 16th, 2008, 06:57 AM
#2
Fanatic Member
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.
-
May 16th, 2008, 07:36 AM
#3
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.
-
May 16th, 2008, 07:48 AM
#4
Fanatic Member
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?
-
May 16th, 2008, 07:54 AM
#5
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.
-
May 16th, 2008, 08:02 AM
#6
Fanatic Member
Re: [2008] Returning multiple values from an Access Database
instead of use
vb Code:
if reader.hasrows() while reader.read() 'your code here end while reader.close end if
-
May 16th, 2008, 08:14 AM
#7
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:35 AM.
-
May 16th, 2008, 08:38 AM
#8
Fanatic Member
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.
-
May 16th, 2008, 09:19 AM
#9
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
I am not sure how to write a collection for this...
???
-
May 16th, 2008, 09:27 AM
#10
Fanatic Member
Re: [2008] Returning multiple values from an Access Database
vb Code:
Public Shared Function ReturnItems(ByVal sGroup As String) As list(of cItemGroup) Dim itemgroup As New cItemGroup dim myList as List(of cItemGroup) = new List(of cItemGroup) Dim connection As OleDbConnection = DBconnect.GetConnection3 Dim sSQL As String = "SELECT Itemcode, M_ItemGroupID " & _ "FROM Cust WHERE M_ItemGroupID = '" & sGroup & "'" Dim runSQL As New OleDbCommand(sSQL, connection) Try connection.Open() Dim reader As OleDbDataReader = runSQL.ExecuteReader(CommandBehavior.SingleRow) while reader.Read itemgroup.Itemcode = reader("Itemcode").ToString 'itemgroup.ItemID = reader("M_ItemID").ToString itemgroup.ItemGroupID = reader("M_ItemGroupID").ToString myList.add(itemGroup) end while reader.Close() Catch ex As OleDbException itemgroup = Nothing Finally connection.Close() End Try Return myList End Function
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click dim myList as List(of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text) for each cig as sItemGroup in myList messageBox.show(cig.ItemCode & " " & cig.ItemGroupID) next cig End Sub
-
May 16th, 2008, 09:41 AM
#11
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 16th, 2008, 09:48 AM
#12
Fanatic Member
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.
-
May 16th, 2008, 11:04 AM
#13
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 16th, 2008, 01:11 PM
#14
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>.
-
May 19th, 2008, 03:17 AM
#15
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 03:20 AM
#16
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 03:51 AM
#17
Fanatic Member
Re: [2008] Returning multiple values from an Access Database
can you post the code you are using at this moment?
-
May 19th, 2008, 04:04 AM
#18
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 04:07 AM
#19
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 04:18 AM
#20
Fanatic Member
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.
-
May 19th, 2008, 04:22 AM
#21
Fanatic Member
Re: [2008] Returning multiple values from an Access Database
replace this code
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim myList As List(Of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text) For Each item As cItemGroup In myList ListBox1.Text = item.Itemcode Next item End Sub
by
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim myList As List(Of cItemGroup) = cDBItemGroup.ReturnItems(Group.Text) me.listbox1.Items.AddRange(myList) End Sub
-
May 19th, 2008, 05:13 AM
#22
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 05:18 AM
#23
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:47 AM.
-
May 19th, 2008, 05:30 AM
#24
Fanatic Member
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:
Me.ListBox1.Items.AddRange(myList.ToArray())
-
May 19th, 2008, 05:47 AM
#25
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 09:22 AM
#26
Fanatic Member
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim input() As String = New String() {"1", "2", "3", "4", "5", "6", "7", "8"}
Dim mylist As List(Of String) = New List(Of String)
'mylist.AddRange(input)
For Each str As String In input
mylist.Add(str)
Next str
Me.ListBox1.Items.AddRange(mylist.ToArray)
End Sub
Use
vb Code:
Dim sCode As String = reader.GetString(0)
instead of
vb Code:
Dim sCode As String = reader("Itemcode").ToString
-
May 19th, 2008, 09:38 AM
#27
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:36 AM.
-
May 19th, 2008, 10:05 AM
#28
Fanatic Member
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:
public class cItemGroup
Public Overrides Function ToString() As String
return me.m_itemCode
End Function
end class
-
May 19th, 2008, 10:28 AM
#29
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:37 AM.
-
May 19th, 2008, 10:50 AM
#30
Fanatic Member
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?
-
May 19th, 2008, 11:02 AM
#31
Thread Starter
Addicted Member
Re: [2008] Returning multiple values from an Access Database
Last edited by robyn.berry; Jun 12th, 2008 at 03:37 AM.
-
May 20th, 2008, 04:50 AM
#32
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|