0

I am not sure how to phrase my question properly but I want to achieve something like this.

I have a class named Products

public class Products

private ID as Integer

private Name as String

Public Property ProductID()
  Get
    Return ID
  End Get
  Set(ByVal value)
    ID = value
  End Set
End Property

In one of my code behind pages, I am retrieving data from an SQL Command and placing the same into a datareader object.

How would I be able to declare the class so that each row in my datareader would actually be an instance of the said class?

Like for example:

Dim myProduct() as New Product
Dim intCnt as Integer 

While datareaderData.read()
  intCnt += 1
  myProduct(intCnt) = new Product
  myProduct(intCnt).ID = datareaderData("ID")
  myProduct(intCnt).Name = datareaderData("Name")
End While

When I do the same, I am getting an error "Object Reference Not Set to an Instance of an Object.

I am quite stumped on this one. Any tips greatly appreciated. Thanks.

1 Answer 1

1

You should use an Arraylist or -better- a generic List(of Product). Besides i would strongly recommend to set Option Strict On in your project's Compiler Settings.

Dim products As New List(Of Product)
While datareaderData.read()
    Dim nextProduct As New Product
    nextProduct.ProductID = CType(datareaderData("ID"), System.Int32)
    nextProduct.Name = datareaderData("Name").ToString
    products.add(nextProduct)
End While
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.