error when trying to fill in textfields

diox

New member
Joined
Dec 26, 2005
Messages
2
Programming Experience
3-5
hi there,

I'm trying to make an simple program that accesses data from a database(that are containing data about my music-cd's).

all the rows are being displayed in an listview;
when i'm trying to fill the selected item in into textfields,
i get an error : The object reference has not been geared to a copy of an object.

here is the piece of my code :

Dim oAlbum As New clsAlbums
For Each olitm As ListViewItem In lstAlbums.SelectedItems
oAlbum = olitm.Tag
Next
txtTitel.Text = oAlbum.name
txtArtist.Text = oAlbum.artist
txtyear.Text = oAlbum.year

i'm not getting what i'm doing wrong here .. :confused:
thx in advance for your help

diox
 
You're doing two things wrong. First off, you have declared your oAlbum variable as type clsAlbums and then you are assigning an Object reference to it. The Tag property is of type Object, which can be anything. You can't just assign any old object to a variable of type cslAlbums. What if you assigned some other type of object to the Tag property? If you know for a fact that the Tag property contains a clsAlbums object then you need to tell the compiler so by casting it:
VB.NET:
                 oAlbum = DirectCast(olitm.Tag, clsAlbums)
That's what's causing your error, but you are making another mistake too. You are using a For Each loop to iterate through all the items in the ListView, but you don't use the oAlbum variable until you've been through all of them, so the oAlbum variable is ALWAYS going to refer to the last item in the ListView after you complete the loop.
 
declaration of my object ?

thx for your reply : i'm only having one more question :
as what do i need to declare my oAlbum?

thx in advance
diox
 
Whatever type of object you are assigning to the Tag property in the first place is the type that you should declare oAlbum as, and you should also cast when you assign to it because the Tag property returns an Object reference.
 
Back
Top