|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Troubles Closing a FormI 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 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 Show quoteHide quote "sajo***@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 > 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.
filesystem.getfiles question
BackGroundWorker.ProgressChanged "Cross-thread operation not valid" at 2nd/3th/... progress sleep? VS2005 Buiild Options Looking for graphic controls to use in .NET compiles as a form -- but not as a control Start Process Convert RTF Text to Plain Text sleep or delay add columns (modify) Datatable AND source MDB |
|||||||||||||||||||||||