Error Provider and textbox databound to property of type Integer : Displaying other message instead of : Input String is not in correct format

Error Provider and textbox databound to property of type Integer : Displaying other message instead of : Input String is not in correct format

Old forum URL: forums.lhotka.net/forums/t/7670.aspx


tutus posted on Tuesday, September 22, 2009

Hi,

I have a textbox "DistanceTextBox" bound to an property Distance of type INTEGER in my business object MasterValue. I also have an error provider on my form whose datasource is MasterValueBindingSource.  MasterValueBindingSource has its datasource set to my MasterValue Instance.

MasterValueBindingSource.DataSource = MyMasterValue

When the user does not include an input in the textbox distance, CSLA displays for me a warning red icon with the generic error: Input String Not in Correct format.

However the client insists that we display the message : This field is required.

I tried going to my textbox validating event and setting this :

myErrorProvider.SetError("DistanceTextBox", "This field is required") But it seems that by the time TextBox_validating executes it's too late and we get, anyway, the generic message: Input string is not in correct format

Can you please suggest a solution either:

1- How to intercept the framework before it displays the generic warning: Input string is not in correct format so that I display the message I want.

Or

2- How to go and change that generic CSLA message which seems to apply when the user does not enter a value in a TextBox which is bound to an INTEGER property.

I appreciate your taking the time to help :)

 

rsbaker0 replied on Tuesday, September 22, 2009

Are you sure CSLA is generating this message?

I get this all the time using DevExpress controls, and the issue is that the value in the TextEdit control (DevExpress version of TextBox) cannot be converted to the bound property type.

For example, if you have bound your TextBox to an Int property, and then you type characters rather than a valid integer in the control, then it can't be converted to an integer and you would get an error like this.

There is no way of posting this value to your BO (short of binding to a string field rather than integer), so you have to handle this i the UI somehow.

JonnyBee replied on Tuesday, September 22, 2009

Hi,

This is actually caused by DataBinding beeing unable to convert the Text value to your datatype. I have seen the same message from standard windows TextBox'es when an explicit type is set (like Int or Numeric). There are 2 ways (that I know of) to handle this:

1. Add code in TextBox Validating event to set the Text property to a valid number.

2. Use nullable types in your BO and in the constructor of your form (after InitializeComponents) add a single line of code that alters the databinding to set null value to string.Empty. That way an Empty textbox will be converted to null-value. Unfortunately - that property does not accept string.Empty in WindowsForms Designer so it must be set manually.

I can post the needed code later today when I get to work.

/jonnybee

JonnyBee replied on Thursday, September 24, 2009

Hi,

Here is sample code for how to use a nullable type property (not nullable backing field) and get correct error messages.

Sample code sets Bindig.NullValue to String.Empty and handle null value in the property get/set in BO and custom ValidationRule.

You may also add your own code to the "Parse" event on the Bindings element but I haven't used that much before.

/jonnybee

tutus replied on Thursday, September 24, 2009

yeah, that s what I used last, u ssolved my problem man, I appreciate :)

tutus replied on Thursday, September 24, 2009

JonnyBee:
Hi,

Here is sample code for how to use a nullable type property (not nullable backing field) and get correct error messages.

/jonnybee

I could not use this line in VB.Net in CSLA 3.5 version :

MethodCaller.CallPropertyGetter(target, e.PropertyName)

any idea pls ?

thanks

JonnyBee replied on Thursday, September 24, 2009

Hi,

Use this:

      Dim value As Int? = _
          CallByName(target, e.PropertyName, CallType.Get) as Int?

Rocky created MethodCaller (amongst other reasons) as a C# version of CallByName method in VB.NET.

/jonnybee

Copyright (c) Marimer LLC