Tuesday, October 16, 2018

Calculate Due and Advance amount in ASP.NET C#

Calculate Due and Advance amount in ASP.NET C#


HTML


 Total Amount : 
                                    <asp:TextBox ID="txtTotalAmount" runat="server" AutoPostBack="true" 
                                        ontextchanged="txtTotalAmount_TextChanged"></asp:TextBox><br /><br />

                                   Paid Amount : <asp:TextBox ID="txtPaidAmount" runat="server" AutoPostBack="true" 
                                        ontextchanged="txtTotalAmount_TextChanged"></asp:TextBox><br /><br />

                                    Due Amount : <asp:TextBox ID="txtDueAmount" runat="server" AutoPostBack="true" 
                                        ontextchanged="txtTotalAmount_TextChanged"></asp:TextBox><br /><br />

                                     Advance Amount : <asp:TextBox ID="txtAdvanceAmount" runat="server" AutoPostBack="true" 
                                        ontextchanged="txtTotalAmount_TextChanged"></asp:TextBox><br /><br />
                            

C# Code

protected void txtTotalAmount_TextChanged(object sender, EventArgs e)
    {
        try
        {

            decimal a=0, b=0, c = 0, d=0;

            if (txtTotalAmount.Text != "")
            {
                a = Convert.ToDecimal(txtTotalAmount.Text);
            }
            if (txtPaidAmount.Text != "")
            {
                b = Convert.ToDecimal(txtPaidAmount.Text);
            }
           
            if (a > b)
            {
                c = a - b;
                d = 0;
                txtDueAmount.Text = c.ToString();
                txtAdvanceAmount.Text = d.ToString();
            }
            else
            {
                if (a < b)
                {
                    d = b - a;
                    c = 0;
                    
                    txtDueAmount.Text = c.ToString();
                    txtAdvanceAmount.Text = d.ToString();
                }
                else
                {
                    c = 0;
                    d = 0;
                    txtDueAmount.Text = c.ToString();
                    txtAdvanceAmount.Text = d.ToString();
                }

            }
        }
        catch (Exception)
        {
            
        }
    }

1 comment: