473,480 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Troubles Closing a Form

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
2 1064
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5795
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
1
2184
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
3980
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
2998
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.