Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a strange behaviour in the binding of some controls in my solution. The control is a TextBox control that handles numeric behaviours. I am working with a decimal textbox for instance. When I do bind the control to the Decimal property (sometimes to the .Text property too) the .NET Framework raises exceptions when the binding occurs. I have created a simple solution that can help understand my problem. The datasource in the solution contains a list of an entity where there are nullable decimal values. When the user goes to one of the rows with a null in the BonoPago column, the program raises an exception. If the user enters a number in any of the two decimal textboxes and presses the command button, multiple exceptions get thrown.
I think I am missing something really simple here, but I can not find anything in google about it.

As AS has stated I am trying now to describe my problem in words. It's a little long but here it goes:


The code I uploaded to
http://expirebox.com/download/5ef0b0047bd988d29a92c3d9dcde7785.html[^]
contains:

1) DecimalTextBox.cs contains the code for the control I want to bind. That file contains multiple classes as I did not want to put many files inside my project.
2) DecimalTextBox is the class in DecimalTextBox.cs I want to bind.
3) DecimalTextBox inherits from NumericTextBox, NumericTextBox from TextBox, and TextBox from System.Windows.Forms.TextBox.
4) NumericTextBox has a get/set property named Decimal of type decimal.
5) I want to bind that property (Decimal) to a decimal property in an entity.
6) I created a sample entity with four fields (in the ClassEstructura.cs class): a string, an enum value for a day of week, a decimal value and a nullable decimal value.
7) I want to bind the decimal value to an instance of DecimalTextBox through the Decimal property.
8) This is done in Line 41 - Form1.cs with the line
this.montoPagoBinding = this.textBoxPago.DataBindings.Add("Decimal", bindingSource, "MontoPago", true, DataSourceUpdateMode.Never, null);
9) I want to bind the nullable decimal value to another instance of DecimalTextBox through the Decimal property (probably it`s not OK as the property does not accept null values).
10) This is done in line 42 - Form1.cs with the line
this.bonoPagoBinding = this.textBoxBono.DataBindings.Add("Decimal", bindingSource, "BonoPago", true, DataSourceUpdateMode.Never, string.Empty);
11) I put the next 2 lines (43 - 44) - Form1.cs trying to parse the null decimal values, but they do not work.
12) In lines 27-36 I create a list of my entities and assign it as a datasource to a binding source.
13) In line 37 I create a handler for the BindingComplete event in which I can get binding exceptions.
14) The bindingSource_BindingComplete method just adds data to the exception and shows a message box with it.
15) That message box were the screen shots I took and uploaded.
16) The problem with the binding appears when I go from a row with all the values set to one in wich the last column does not have value (it`s null).
17) For instance if I click in the Diego row and later in the Ariel row, the exception is raised and the message box appears.
18) The exception is

Excepción nivel (1):
Tipo de excepción:
System.ArgumentException
ArgumentException.ParamName:
Mensaje:
Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
Source:
System
TargetSite:
Void SetValue(System.Object, System.Object)
StackTrace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object value)
at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
Datos adicionales:
Control.Name = 'textBoxBono'
Control.Type = 'WindowsFormsApplication3.DecimalTextBox'
PropertyName = 'Decimal'
FormatString = ''
FormattingEnabled = '1'
title = NULL

19) The other problem is when I change the value in the decimal control (not the nullable decimal value).
20) For instance if I click in the Diego row, change the value in the third control (labeled A pagar) and press the button1 button, the exception is raised and the message box appears.
21) The exception is

Excepción nivel (1):
Tipo de excepción:
System.ArgumentException
ArgumentException.ParamName:
Mensaje:
Object of type 'System.String' cannot be converted to type 'System.Decimal'.
Source:
System
TargetSite:
Void SetValue(System.Object, System.Object)
StackTrace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object value)
at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
Datos adicionales:
Control.Name = 'textBoxPago'
Control.Type = 'WindowsFormsApplication3.DecimalTextBox'
PropertyName = 'Decimal'
FormatString = ''
FormattingEnabled = '1'
title = NULL

And my problem is I can not understand why the .Net Framework is raising that exception shown by the messagebox.show in line 96 - Form1.cs in the bindingSource_BindingComplete method.
It does not help me with the string value the computer is trying to convert.
And the stack trace sets the error not in my code (I think) but in the framework.
I know I am doing something wrong about it, but honestly I don´t know what it is.
I tried debugging the Framework code, but those methods are optimized and they do not show the values of the variables in debugging time so I can't know what value the program is trying to convert from string to decimal.

I hope this description is more understandable of what my problem is.


Thanks in advance.
Regards.
Ariel.
Posted
Updated 22-Aug-14 3:12am
v4
Comments
[no name] 21-Aug-14 10:07am    
Don't expect too many people to download your project to debug it for you.
Sergey Alexandrovich Kryukov 21-Aug-14 12:14pm    
I actually just downloaded it, and found that, in addition to some source code, there are some *.EXE files. This is nasty.
@Ariel:

Ariel,

You should never ever do it. Additionally, you should figure out precisely what is your source code and what is not bin, obj, *.suo — get rid of it all. You need to figure it all before doing any programming, otherwise... you are not really doing programmin.

—SA
abacrotto 21-Aug-14 15:04pm    
You are right Wes. I will make that upload unavailable and upload a new one without the binaries. I did not know I should not upload binaries. Sorry for that.
The new link is
http://expirebox.com/download/c28bd7c959f26ce328bb4d26e16bddd1.html

Thanks for the advice.
Regards.
Ariel.
Sergey Alexandrovich Kryukov 21-Aug-14 12:11pm    
There is no one general "binding" concept in .NET. This notion is different in different technologies, in particular, different application types. You need to provide relevant information.
—SA
abacrotto 21-Aug-14 15:09pm    
Sergey, can you tell me what other relevant information are you needing ? I think that if you compile the code I uploaded now (the new one does not have the binaries) and run it for a while you will see the exceptions thrown showing in MessageBoxes. My problem is I do not understand why they appear as they are thrown from the .NET Framework and not from my code. I will add a screen capture of one of such message boxes.

Thanks in advance.
Ariel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900