Troubles Closing a Form

S

sajones

Hi,

I have a simple routine that populates a textbox with the value selected on
a listbox:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged

'Displays specific field of list selection
Dim drv01 As DataRowView = DirectCast(lstUsers.SelectedItem,
DataRowView)
Me.txtSelection.Text = drv01("intUserID").ToString()

End Sub

It works great.

However, when I close the form, it errors on the value assignment with a
'System.NullReferenceException was unhandled' error.

In all reality, I don't even want it to try to do the assignment when I'm
closing the form. I'm guessing that the error has something to do with the
fact that the DataRowView is in the process of closing and can't be
referenced. Regardless, does anybody have any suggestions for getting
past/around this error?

Thanks,
Scott
 
G

Guest

Check everything is not nothing before you use it or put a try block around
the code. Sometimes those dim shortcuts will cause problems if you are
accessing a property of a variable that is nothing, selected index could be
nothing.

Good Luck
DWS
 
C

Chris Dunaway

It's possible that when you close the form, it is causing the list box
to clear which would cause the selectedIndexChanged event to fire
again.

You might try first checking that drv01 is not nothing before you do
the assignment and secondly, you might set a boolean flag (e.g.
bClosing = True) when you close the form. Then in the
SelectedIndexChanged event, check that flag and if true, dont execute
the code just exit the event.

Hope this gives you some ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top