Links Required for Higher Page Rank Advanced ASP.NET Docking Panel
Apr 25

Printer Friendly Version

Download Source Code: TaxCalculator.zip - 14.88KB

Canadian Income Tax Calculator in ASP.NET

You may want to provide a server-side solution, with code hidden by your web server. Try it as a generic online tool.

The Default.aspx page has as code-behind a similar GetResult function in C#, which populates the content of a PlaceHolder with a very similar table as the JavaScript-based solution. Tax-related parameters are stored now in a bidimensional array:

/// <summary>
/// Returns a HTML table with income taxes
/// per province for entered taxable income
/// </summary>
/// <returns></returns>
public string GetResult()
{
    // if invalid income value
    string s = "Taxable Income: $1 .. $10,000,000";
    if (salary <= 0 || salary > 10000000)
        return s;

    double taxF = 0;
    s = "<table cellspacing=\"5\" "
        + "style=\"border:solid 1px gainsboro; font-size:80%;\">"
        + "<tr><th>Province</th>"
        + "<th>Prov Taxes</th><th>Fed Taxes</th>"
        + "<th>Net Income</th></tr>";

    // for each province entry (including Federal)
    for (int i = 0; i < 14; i++)
    {
        if (taxF > 0)
            s += "<tr><td><i>" + taxes[i, 0] + "</i></td>";

        double tax = 0;
        double left = salary;
        for (int j=1; left > 0; j += 2)
        {
            int bracket = Convert.ToInt32(taxes[i, j + 1]);
            if (bracket > 0 && left >= bracket)
            {
                // for specific bracket
                tax += Convert.ToDouble(taxes[i, j]) * bracket;
                left -= bracket;
            }
            else
            {
                // for all left amount
                tax += Convert.ToDouble(taxes[i, j]) * left;
                left = 0;
            }
        }

        if (taxF == 0)
            taxF = tax;
        else
            s += "<td>" + tax.ToString("$#,###,##0")
                + " (" + (tax / salary).ToString("#0.00%")
                + ")</td><td>" + taxF.ToString("$#,###,##0")
                + " (" + (taxF / salary).ToString("#0.00%")
                + ")</td><td><b>" + (salary - tax - taxF)
                    .ToString("$#,###,##0")
                + "</b> (" + ((salary - tax - taxF) / salary)
                    .ToString("#0.00%")
                + ")</td></tr>";
    }
    
    return s + "</table>";
}

Subscribe and Share: Subscribe using any feed reader Bookmark and Share

Leave a Reply