tl;dr Method of copying contents of an SQL query to an array or a string or a listbox. Single columns, multiple rows.
Working on a small project for some extra course credit.
Developing it currently in Visual Studio 2010.
Essentially is an interactive menu where users select items and can add them to an inbuilt
list and it will calculate total nutritional information and costs etc..
I’m having an issue however. When the user reaches the order builder page they can select the type of item they wish to buy.
E.G.
Beef
Clicking this should then populate a list box with all the related items.
I’m hoping to do this via a database connection.
I currently have an embedded database.
Their where 2 ways I’ve tried to do this but both proved unsuccessful or perhaps I’m just doing it wrong.
First method.
Dim index As Integer = 0
Dim length As Integer = adapter.productscounter()
' Small query that works out total number of rows.
For index = 0 To length
ListBox1.Items.Add(adapter.SelectBeef(index))
Next
This gives me the error:
There is no row at position 0.
which I seem unable to solve. The query runs upon trial execution and theirs something their.
Index out of range exception
The other method I was attempting was similar code however using an array and then copying the contents of that into the listbox.
Dim index As Integer
Dim test(5)
Dim length As Integer = adapter.productscounter()
Dim counter As Integer
For index = 0 To length
test(index) = adapter.SelectChicken()
counter = counter + 1
Next
For counter = 0 To length
ListBox1.Items.Add(test(index))
Next
Generates:
Argument nullexception
Value cannot be null.
Parameter name: item.
In Visual Basic, the standard is to start a list with element 1, not element 0. You could try:
Though of course, I have no insight in what the
SelectBeefmethod does.