Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day,
I have developed and application on a 64 bit OS, WIN 8, it is running perfect but after I installing the app on 32 bit, WIN 7 machine I experience an error which I honestly find it weird. I have selected "Release" and "86x" on my Visiual studio. Your urgent response will be appreciated

my Code
C#
namespace AfricanFoodsPOS
{
    public partial class Payment : Form
    {
        private BindingList<tblproduct> products = new BindingList<tblproduct>();

        AFStagingEntities afNewObject = new AFStagingEntities();

        public delegate void PaymentMadeEvent(object sender, PaymentMadeEventArgs e);

        //public decimal myValue = 0;

        public event PaymentMadeEvent PaymentMade;

        public decimal myPay;
        public decimal MyPay
        {
            get { return myPay; }
            set {
                  myPay = value;
                  txtYouArePaying.Text = String.Format("{0:c}", myPay);                 
                } 
        }

        public decimal paymentAmount;
        public decimal PaymentAmount
        {
            get { return paymentAmount; }
            set
            { 
                paymentAmount = value;             
                txtAmountToPay.Text = String.Format("{0:c}", paymentAmount);                
            }
        }       

        public Payment()
        {
            InitializeComponent();         
              
        }


        //was private
        public void PaymentHasBeenMade(object sender, EventArgs e)
        {
            decimal total = 0;           
            //robust
            try
            {
                decimal YouArePaying = 0;
                decimal AmountToPay = 0;
                YouArePaying = decimal.Parse(txtYouArePaying.Text.TrimStart('R'));
                AmountToPay = decimal.Parse(txtAmountToPay.Text.TrimStart('R'));

                total = YouArePaying - AmountToPay;

                if (total >= 0)
                {
                    MessageBox.Show("Give change: R " + total);
                    PaymentMade(this, new PaymentMadeEventArgs() { PaymentSuccess = true });
                }
                else
                {
                    total = AmountToPay - YouArePaying;
                    txtAmountToPay.Text = total.ToString();

                    MessageBox.Show("Customer is: R " + total + " short.");
                    PaymentMade(this, new PaymentMadeEventArgs() { PaymentSuccess = false });
                }
            }
            catch (Exception exx)
            {
                MessageBox.Show("An error has occured, please enter a valid amount, " + exx.Message);
            }

            finally
            {
                //txtYouArePaying.Dispose();          
            }
        }


        private void Payment_Load(object sender, EventArgs e)
        {

        }     

    }


    public class PaymentMadeEventArgs : EventArgs
    {
        private bool paymentSuccess;

        public bool PaymentSuccess
        {
            get { return paymentSuccess; }
            set { paymentSuccess = value; }
        }
    }

    
}
Posted
Updated 15-Oct-15 22:42pm
v2
Comments
Andy Lanng 16-Oct-15 4:45am    
Hi,

Please use the 'Improve Question' button above this comment and add to your post:
1: The error message as it appears
2: The exact line at which the error occurs

Thanks ^_^
Baitshoki 16-Oct-15 5:08am    
The problem is it is running fine on my machine but when trying to run on the other machine after installation it gives me that error. so I cant really say which line of code is throwing an error but it happens after I input amount in a textbox "txtAmountToPay.Text" and click a "payment" button. It is not accepting input to that text field. So I assume its on the code below:

AmountToPay = decimal.Parse(txtAmountToPay.Text.TrimStart('R'));




Andy Lanng 16-Oct-15 5:10am    
Ah ha - yes, I agree. That line of code it likely to error. You should always sanitize your inputs. I'll add code in solution as an example.

1 solution

As per our comments, this solution highlights how to sanitize your data inputs if you want to parse them as numeric.

First: It's always best to use numeric inputs or regex's for numeric controls. This helps reduce the issues on the server side and can provide meaningful and responsive information to the user. It's not always possible to do this, so this is how you can handle it server side

(Oh, NB, I'm currently a web developer, so when I say "server side", this could also refer to the code behind your control as apposed the the code controlling the behavior of the control, which I guess I'd call client side)

So: Parsing numeric values. int.Parse will pick out the integers from a string, regardless of letters or anything else, even decimal points. double.Parse cannot ignore the other digits because they could be relevant to the structure of the number. If the structure is not sound, then it throws an error.

You can check if the value is parse-able by using double.TryParse. This will return a boolean value: True=can parse and false=can't parse. You also pass an out parameter so if it can parse, you also get the result.

Take a look:
C#
string alphanum = "123.abc";
string justnum = "123.456";

double result; //Don't bother setting the out parameter.  It will always change in the tryparse

if(double.TryParse(alphanum,out result){
    Assert.Fail("The parse should return false);
}
Assert.AreEqual(0.0,result);  //whatever the 'result' was set to before, it is now zero
if(double.TryParse(justnum ,out result){
    Assert.AreEqual(123.456,result);
}



Using this you can check your data before using it. So if the tryparse fails, tell the user that the format it wrong

I hope that helps ^_^
Andy
 
Share this answer
 

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