By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Troubles Closing a Form

P: n/a
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
Feb 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
DWS
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


"sa*****@netta.us" wrote:
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

Feb 14 '06 #2

P: n/a
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.

Feb 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.