Error: Input String was not in correct format when a field is blank

Error: Input String was not in correct format when a field is blank

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


davidng posted on Friday, February 11, 2011

Hi,

I received this error "Input String was not in correct format" when I manually clear out this field.

In Validation section of a BO, I define as MaxLength:

ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("Proposal_Received", 2))

I have 2 questions:

Q1: Why error happen?
Q2: To debug this error, where should I set the breakpoint?

Thank you in advance for your help.

ajj3085 replied on Saturday, February 12, 2011

What UI are you using?  In WinForms you need to set the empty value on the control to empty string instead of null, otherwise you might get this exception.

JonnyBee replied on Saturday, February 12, 2011

Basically what Andy said. This message is usually caused by DataBinding beeing unable to convert the UI controls value to the BO property type value.

1. You can change the "null" value on the databinding:
      (ex:change from null to string.Empty)
      nameTextBox.DataBindings["Text"].NullValue = string.Empty;

2. You can add hook into the Parse event on the databinding to transform the input value to your property type.

davidng replied on Monday, February 14, 2011

Hi Johnyy,

The object property is a Decimal type. In Data Binding - Advance window, I set NullValue to 0. Please see my previous reply.

And I still got the error "Input string was not in correct format"

Am I missing something else? Please advise.

JonnyBee replied on Monday, February 14, 2011

The DataBinding.Advanced window is not advanced enough.

You need to set this in the constructor - after InitializeComponents has been called.

So like this:
      <control>.DataBindings["Text"].NullValue = string.Empty;

or add your own Parse method to the databinding.

davidng replied on Monday, February 14, 2011

Hi Johnny,

I did put the code in CONSTRUCTOR as follow.

 InitializeComponent()

 

 

 Me.txtContrPropReq.DataBindings("Text").NullValue = 0

However, I still get that error message. By the way, value is Decimal Type, therefore, I use 0, instead of String.Empty.

Thanks.

 

JonnyBee replied on Monday, February 14, 2011

I'm guessing the problem is caused by txtContrPropreq.Text  is string.Empty and the default parser is not able to convert the string.Empty into a numeric value. If your decimal property is nullable then  setting Nullvalue to string.Empty will instruct the default parser to convert an empty string (control.Text)  to null when updating the model.

Your code instruct databinding to convert null-value into 0 (as text) and parse 0 (control.Text) into null when setting the property. If your control.Text is string.Empty the databinding is not able to convert the value and thus will throw exception "Input string was not in correct format".

Is your property Nullable?

If this is not correct behavior then add your own parse and/or formatting implementation.

DataBinding has 2 important events:
* Binding.Format     - converts data value to display value (typically control.Text)
* Binding.Parse       - converts text (edited) value to data value

Read more here: http://en.csharp-online.net/DataViews_and_Data_Binding%E2%80%94Format_and_Parse

davidng replied on Tuesday, February 15, 2011

Hi Johnny,

Many thanks for your help. The problem is finally sovled.

ajj3085 replied on Tuesday, February 15, 2011

JonnyBee

The DataBinding.Advanced window is not advanced enough.

You need to set this in the constructor - after InitializeComponents has been called.

So like this:
      <control>.DataBindings["Text"].NullValue = string.Empty;

or add your own Parse method to the databinding.

Jonny, are you sure about that?  I did a lot of databinding in WinForms and never had to do that, and the code you're suggesting is pretty much exactly what gets generated in the .designer.cs file. 

Except for interacting with bindingsource components or one special case i had to have a modified Binding for a special case, setting the bindings in the designer was all that was required.

JonnyBee replied on Tuesday, February 15, 2011

In the designer.cs I have not found a way to set the NullValue to string.Empty. NullValue is default null.

Especially on numerical nullable properties I've had to add that line of code to make an "empty" value be handled as null.

davidng replied on Friday, February 25, 2011

Hi Johnny,

If the property is of type DATE or SMART DATE, then do you handle differently.

I thought I solved the problem, but when the property is SMART DATE, I encounter error again.

I add breakpoint at SET property, but it never got extecuted.

Please advice. 

RockfordLhotka replied on Friday, February 25, 2011

It is important to understand that SmartDate is not intended to be used as a public type. It is intended to be used as the backing field type behind a string property.

In some limited scenarios you might expose a public SmartDate type property, but usually not if you intend to bind that property to the UI.

The intent behind SmartDate is to make it easy to implement a string property where the user can type in values that can be somehow interpreted or parsed as a date, or as an empty value as the string value is set into the SmartDate backing field.

davidng replied on Friday, February 25, 2011

Hi Rocky,

Thank you very much for your promptness response.  I now understand better.

davidng replied on Monday, February 14, 2011

I am using WinForm.

In Data Binding - Advance window, I set Null value to 0. The Designer generated code:

 

 

 

Me.txtContrPropReq.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.ObContractBindingSource, "Proposal_Received", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged, 0))

 

I still got the error: " Input String was not in correct format"

 

 

 

Copyright (c) Marimer LLC