Widunder Posted November 24, 2011 Posted November 24, 2011 Hello everyone, I have three different tables. Customer, Product and Orderline. Now, I want to find how to retrieve the information from a product object in order to insert that data into the orderline. My product object has pName as a key, and imagePath and pPrice as attributes. But all I want - is to get the price, to be returned as a decimal value. This is what I've done so far and it's not working: Public Function priceLookUp(ByVal pName As String) Dim SQL As String = "select pPrice from product where pName = '" & pName & "'" Dim dtProducts As DataTable = myData.getData(SQL) Dim colProducts As Collection = New Collection Dim objProduct As Product Dim drRow As DataRow For Each drRow In dtProducts.Rows objProduct = New Product 'empty object being created? objProduct.ProductPrice = drRow.Item("pPrice") If colProducts.Contains(pName) = True Then objProduct = colProducts("ProductName") Else End If Next Return colProducts("pPrice") 'this line is the one that I have problem with End Function I'm having the problem with the last line. I'm getting the correct product name - finding it's key, but how do I get the price of that product? I've tried to modify that line heaps of times now, and I still just cant figure it out. I've tried drRow.Item("pPrice") - that didn't give me any errors, though at the same time, it didn't add nothing to the orderline table. Because I was guessing that an empty object of product is being created. But how to avoid this? Please help. Thank you. Widunder. Quote
Administrators PlausiblyDamp Posted November 24, 2011 Administrators Posted November 24, 2011 You might want to switch from using the Collection class to a more modern replacement such as List(Of ...) e.g. Dim colProducts As List(Of Product) = New List(Of Product) then in the final line you could do something like colProducts("pPrice").ProductPrice However if you are just returning a single price from a single product then you can probably simplify the code considerably as there is no need for a collection of products to return a single product price. e.g. Public Function priceLookUp(ByVal pName As String) as Decimal Dim SQL As String = "select pPrice from product where pName = '" & pName & "'" Dim dtProducts As DataTable = myData.getData(SQL) Dim drRow As DataRow = dtProducts.Rows(0) Return DirectCast( drRow("pPrice"), Decimal) End Function That might not be perfect as I don't have VB on this PC at the moment but it should get you started. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.