none
Nullable decimal problem RRS feed

  • Question

  • Hi,

    In my sql server database, I have a AccountBalance field that is defined as money and allow null. In the entity model, the data type is translated as decimal with Nullable = none, precision = 19 and scale = 4.

    I am using self tracking. In the model.tt file, the AccountBalance is a type of Nullable<decimal>.

    I bind this field with a textbox. I have two problem. first it displays 4 digits after decimal point (I want it to be 2 digits). Is this number of digits defined by the Scale in the properties for the AccountBalance? But I checked the definition of the AccountBalance in the model.tt file, it doesn't specify how many digits after the decimal point.

    Second problem: when I change the value, like from 2.0000 to 4.0000, then click on other textbox, the value will change back to 2.0000. This is caused by nullable type.

    The question is how normally we should handle this nullable money field? If I have to change the entity model, that will be disaster for me, because I have 98 tables. If I changed the database I will have to manully change all of these again and again.  

    Thursday, March 8, 2012 1:35 AM

Answers

  • Hi Peter,

    Welcome!

    I think you should call AcceptChanges() method on each Client in List<Client> in client ;

     using (var service = new Service1Client())
                {
                    foreach (var client in ListClient)
                    {
                        service.UpdateDepartment(client);
                      client.AcceptChanges();                
                    }
                }

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Wednesday, March 14, 2012 8:11 AM
    Moderator
  • You can take a field of type double and convert it to a formatted string as shown here.

    The 2nd problem sounds to me like the text value is not bound correctly.  If the binding is an ObjectSet<ofSomeType> you may need to convert it to IENUMERABLE<ofSomeType>  also, check the bind MODE, set it to TWO WAY.


    JP Cowboy Coders Unite!

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Thursday, March 8, 2012 1:40 AM
  • Hi

    For first one,  when you say "(I want it to be 2 digits)", you are looking to rounding into 2 digits or on display only show 2 digits?

    If you are looking for rounding, I would suggest in best place in property

     decimal? money;
            public decimal? Money {
                get { return money; }
                set 
                {
                    money = value == null ? value :decimal.Round(value.Value,2); 
                }
            }

    If you are looking for only on display,

    format the value before you bind to textbox

     textBox1.Text = string.Format("{0:0.##}", 0.345);

    For second one, looks like binding issue. this is nothing to do with nullable type. if you provide code part of binding may be helpful to solve your issue..

    Hope this helps you


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Thursday, March 8, 2012 3:58 AM

All replies

  • You can take a field of type double and convert it to a formatted string as shown here.

    The 2nd problem sounds to me like the text value is not bound correctly.  If the binding is an ObjectSet<ofSomeType> you may need to convert it to IENUMERABLE<ofSomeType>  also, check the bind MODE, set it to TWO WAY.


    JP Cowboy Coders Unite!

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Thursday, March 8, 2012 1:40 AM
  • Hi

    For first one,  when you say "(I want it to be 2 digits)", you are looking to rounding into 2 digits or on display only show 2 digits?

    If you are looking for rounding, I would suggest in best place in property

     decimal? money;
            public decimal? Money {
                get { return money; }
                set 
                {
                    money = value == null ? value :decimal.Round(value.Value,2); 
                }
            }

    If you are looking for only on display,

    format the value before you bind to textbox

     textBox1.Text = string.Format("{0:0.##}", 0.345);

    For second one, looks like binding issue. this is nothing to do with nullable type. if you provide code part of binding may be helpful to solve your issue..

    Hope this helps you


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Thursday, March 8, 2012 3:58 AM
  • Hi,

    Thank both of you for the help.

    For second one, looks like binding issue. this is nothing to do with nullable type. if you provide code part of binding may be helpful to solve your issue..

    TextClientName.DataBindings.Add("Text", ClientRows, "Name"); where ClientRows is List<Client> returned from WCF service.

    Monday, March 12, 2012 2:53 AM
  • Hi Peter,

    Welcome!

    I think you should call AcceptChanges() method on each Client in List<Client> in client ;

     using (var service = new Service1Client())
                {
                    foreach (var client in ListClient)
                    {
                        service.UpdateDepartment(client);
                      client.AcceptChanges();                
                    }
                }

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by peter 9 Wednesday, March 14, 2012 10:17 PM
    Wednesday, March 14, 2012 8:11 AM
    Moderator
  • Hi,

    What I do is changing data type from money to decimal(12,2) in my sql server tables.

    Thank all of you for the helps.

    Wednesday, March 14, 2012 10:17 PM
  • Hi,

    I changed the data type from money to decimal and this fixed my second question. But now I found some nullable int field cannot be changed to Not nullable int because the business logical reason. So I tried to use your suggestion but I don't know how to do it.

    I work on winform project.

    I want to display a medicare position number, that is an integer on a textbox.

    TextMediPos.DataBindings.Add("Text", PatientRows, "MediPos"); where PatientRows is List<patientservicetypes.patient>PatientRows, that comes from WCF service.

    Questions are

    1) Do I still need convert PatientRows to IEnumerable?

    2) bind mode seems in WPF. What should I do in winform?

    Thank you very much.

    • Edited by peter 9 Monday, July 16, 2012 11:43 PM
    Friday, July 13, 2012 1:09 AM