Solved

Validation problems when users blank out a textbox bound to a number field in vb.net windows app.

Posted on 2008-09-30
6
301 Views
Last Modified: 2013-11-26
In my VS 2008 vb.net project I have some textboxes bound to number fields in the DB. when the user enters a number, then blanks out the number and moves off the textbox and tries to save the record it fails because it trys to save an empty string in the record.
How can I best handle this?

thanks,
bmutch
0
Comment
Question by:PACCAST-DEV
  • 4
6 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
Comment Utility
What you need is to hook the Parse event and in the event handler check the
argument value. If it is a string and empty, set the value to System.DBNull.Value.

Binding aBinding = new Binding("Text", your_datasource,"your field name");
aBinding .Parse +=new ConvertEventHandler(Parse_Handler);


Source: http://www.sitepoint.com/forums/showthread.php?t=289691
Private Sub Parse_Handler(sender As Object, e As ConvertEventArgs) 

    If e.Value.[GetType]() = Type.[GetType]("System.String") Then 

        Dim s As String = DirectCast(e.Value, String) 

        If s.Trim().Length <= 0 Then 

            e.Value = System.DBNull.Value 

        End If 

    End If 

End Sub 

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
That is one method if you want NULLs in the field.

Generally I handle this through the LostFocus event.  I check if the value is valid and if not don't let them leave the textbox until they put a valid value in it.

mlmcc
0
 

Author Comment

by:PACCAST-DEV
Comment Utility
Yes, I do allow nulls in these number fields.
Dhaest: - I have to declare this binding variable for every (about 10) field I want to take care of in this manner?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:PACCAST-DEV
Comment Utility
where does this line go:
aBinding .Parse +=new ConvertEventHandler(Parse_Handler);
 and this line gives error:
If e.Value.[GetType]() = Type.[GetType]("System.String") Then
error : operator = is not defined for types system.type...
0
 

Author Comment

by:PACCAST-DEV
Comment Utility
looks like I got it working, lot of hassle to just take care of the empty textbox...
0
 

Author Comment

by:PACCAST-DEV
Comment Utility
I used a variation of the "Binding" method above. Am dissappointed that this is so hard to do in VS...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now