Looping through listbox making calculation to every item - wont update new result.

Faultyboy

New member
Joined
Apr 6, 2009
Messages
1
Programming Experience
3-5
Hello,

I hope one of you can please help me with a small problem i am having....

Ok i have 2 list boxes.. Qty and Cost both of these are populated from a sql database. The code is suppose to read through the listboxes and multiple the Cost by the Qty and then replace the Cost with the new result. The Cost listbox is populated with the cost of one item. The reason i am not doing this calculation in the sql query is because the sql query is very complicated filter out a bunch of stuff to show just the qty of the item...

sql query = "SELECT DISTINCT Trans.TransDescription, Count(Trans.TransDescription) AS Qty FROM Trans WHERE Debit = '0' AND Checked = '0' GROUP BY Trans.TransDescription"

ok here is the vb.net code everything works fine it just wont update the selected itme with the new value..

Dim Y As Integer = 0
Dim Z As Object
Dim ZZ As Object
Dim ZZZ As Object

Try
Do Until Y = LbCurrentAccountQty.Items.Count
LbCurrentAccountQty.SelectedIndex = Y
LbCurrentAccountCost.SelectedIndex = Y
Y = Y + 1
Z = LbCurrentAccountQty.SelectedItem
ZZ = LbCurrentAccountCost.SelectedItem
ZZZ = (Z * ZZ)
'Code works up until here. It just does not want to update the cost.
LbCurrentAccountCost.SelectedItem = ZZZ

Loop
Catch ex As Exception
MsgBox(ex.Message)
End Try

Thanks very much for your help
 
First I dont see a cost being returned in your query at all. Only a quantity and description... Also I think your query could be modified to pass back the info you want but to answer your question you also dont have to loop through each record to get these values either.

Ok your example doesnt show whether the query results are stored in a dataset, datatable or even just a reader which then added them to a listbox. I'm assuming a reader since your navigating the listbox items instead of a tables rows.

I would suggest changing this to use a Dataset or DataTable and adding an additional column to your table to hold this calculated value. After your fill your table with your query results all you have to do is set an expession on the column.

VB.NET:
myDataset.Tables("TableName").Columns("ColumnName").Expression = "Qty * Cost"

Just some added advice, its very bad coding practice to name your variables something as unmeaningfull as y,z,zz,zzz. Theres also doesnt seem to be any reason that these datatypes are objects. Your trying to figure out a cost so use an appropiate datatype to represent such a cost such as a decimal.
 
Back
Top