[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1500 total points
ID: 22612002
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 101

Expert Comment

by:mlmcc
ID: 22614594
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
ID: 22615062
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:PACCAST-DEV
ID: 22615148
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
ID: 22616983
looks like I got it working, lot of hassle to just take care of the empty textbox...
0
 

Author Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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