Databinding and DBNull value

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 19-Dec-2005 10:39:51   

I've an entity with some decmial fields, which are bound to a Infragistics UltraTextEditor and UltraNumericEditor, and I set Nullable propery of those editor controls to true. When I delete the value from control, it throws the following exception.


Target:
System.Object CheckValue(System.Object, System.Reflection.Binder, System.Globalization.CultureInfo, System.Reflection.BindingFlags)

Error:
Object of type 'System.DBNull' cannot be converted to type 'System.Decimal'.

Stack Trace:
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityPropertyDescriptor2.SetValue(Object component, Object value)
   at System.Windows.Forms.BindToObject.SetValue(Object value)
   at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)

I try to bind it to standard TextBox, it also throw exception when I delete text from control.


Target:
Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)

Error:
Input string was not in a correct format.

Stack Trace:
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Decimal.Parse(String s, NumberStyles style, IFormatProvider provider)

However, when I bind the field in UltraGrid, it does not have any exception if I delete value from cell. Could anyone show me some guidelines how to handle DBNull value? Thank you very much.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Dec-2005 14:52:08   

Error: Object of type 'System.DBNull' cannot be converted to type 'System.Decimal'.

That's since the databinding is trying to assign a Decimal a value of DBNull which is not possible, you should let it or assign Zero or nothing at all.

And if you want to set null in the database, you should check for Zero and the use SetNewFieldValue() function to set it to null.

The following thread will be of much help: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=4392

Good Luck