如果返回错误页面,c#就会出错

本文关键字:出错 返回 错误 如果 | 更新日期: 2023-09-27 18:12:11

我正试图用if else语句来处理不同的事情。我需要一个拉下拉列表的值来确定计算将是什么,另一个拉复选框的值并分配计算,另一个用于输入验证,如果数字超过32,最后一个给我一个由输入到文本框中的数量(最多32)确定的值。我想我已经得到了复选框语句更正(guideFee),输入验证和一个与它一起去似乎是正确的已经,但我有麻烦分配恒定的十进制值下拉列表值,我不明白为什么。我有basetourRate = Convert.ToDecimal(riverTour)和后面的if语句,但是当我运行程序时,我得到一个错误页面。我粘贴了整个代码。如果有人能帮助我,我将非常感激,谢谢!

{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void butCalculate_Click(object sender, EventArgs e )
    {
        //1. Declare Variables
        decimal totalBaseTourFee;
        decimal totalGuideFee;
        decimal totalWeekendSurcharge;
        decimal subtotal;
        decimal salesTaxCharge;
        decimal totalCharge;
        decimal baseTourRate;         
        decimal guideFee = 0m;
        bool isGuidedTour;
        int numberOfRafters;
        int numberOfRaftsNeeded; 
        string riverTour;
        string timeOfWeek;
        string displayTotalBaseTourFee;
        string displayNumberOfRaftsNeeded;
        string displayTotalGuideFee;
        string displayTotalWeekendSurcharge;
        string displaySubtotal;
        string displaySalesTaxCharge;
        string displayTotalCharge;
        const decimal FRENCH_BROAD = 40m;
        const decimal NANTAHALA = 30m;
        const decimal TUCK = 20m;
        const decimal WEEKEND_SURCHARGE = 10M;
        const decimal GUIDE_FEE = 35M;
        const decimal SALES_TAX_RATE = .07M;
        //2. Get Values
        numberOfRafters = Convert.ToInt32(txtNumberOfRafters.Text);
        riverTour = ddlTour.SelectedValue;
        isGuidedTour = chkGuided.Checked;
        timeOfWeek = rblDay.SelectedValue;
        Trace.Warn("numberOfRafters = " + numberOfRafters);
        Trace.Warn(("isGuidedTour = " + isGuidedTour));
        Trace.Warn("riverTour = " + riverTour);
        Trace.Warn("timeOfWeek = " + timeOfWeek);
        //Input Validation
        if (numberOfRafters > 32)
        {
            Console.WriteLine("Sorry, we can't handle this large of a group!");
            return;
        }
        //3. Do Calculations
        baseTourRate = Convert.ToDecimal(riverTour);
        if (riverTour == "FB")
        {
            baseTourRate = FRENCH_BROAD; 
        }
        else if (riverTour == "NH")
        {
            baseTourRate = NANTAHALA;
        }
        else if (riverTour == "TK")
        {
            baseTourRate = TUCK;
        }

        if (isGuidedTour == true)
        {
             guideFee = GUIDE_FEE;
        }
        else if (!isGuidedTour == false)
        {
            guideFee = 0;
        }
        totalBaseTourFee = numberOfRafters * baseTourRate;
        numberOfRaftsNeeded = numberOfRafters;
        if (numberOfRafters <= 8)
        {
            numberOfRaftsNeeded = 1;
        }
        else if (numberOfRafters <= 16)
        {
            numberOfRaftsNeeded = 3;
        }
        else if (numberOfRafters <= 24)
        {
            numberOfRaftsNeeded = 3;
        }
        else if (numberOfRafters <=32)
        {
            numberOfRaftsNeeded = 4;
        }
        totalGuideFee = numberOfRaftsNeeded * guideFee;
        totalWeekendSurcharge = numberOfRafters * WEEKEND_SURCHARGE;
        subtotal = totalBaseTourFee + totalGuideFee + totalWeekendSurcharge;
        salesTaxCharge = subtotal * SALES_TAX_RATE;
        totalCharge = subtotal + salesTaxCharge;
        //4. Display Results
        displayTotalBaseTourFee = totalBaseTourFee.ToString("C") + "<br>";
        displayNumberOfRaftsNeeded = numberOfRaftsNeeded.ToString("C") + "<br>";
        displayTotalGuideFee = totalGuideFee.ToString("C") + "<br>";
        displayTotalWeekendSurcharge = totalWeekendSurcharge.ToString("C") + "<br>";
        displaySubtotal = subtotal.ToString("C") + "<br>";
        displaySalesTaxCharge = salesTaxCharge.ToString("C") + "<br>";
        displayTotalCharge = totalCharge.ToString("C") + "<br>";
        lblTotalBaseTourFee.Text = displayTotalBaseTourFee;
        lblNumberOfRafts.Text = displayNumberOfRaftsNeeded;
        lblTotalGuideFee.Text = displayTotalGuideFee;
        lblTotalWeekendSurcharge.Text = displayTotalWeekendSurcharge;
        lblSubtotal.Text = displaySubtotal;
        lblSalesTaxCharge.Text = displaySalesTaxCharge;
        lblTotalCharge.Text = displayTotalCharge;
    }
    protected void butClear_Click(object sender, EventArgs e)
    {
        //Clear TextBox and Label
        txtNumberOfRafters.Text = "";
        lblTotalBaseTourFee.Text = "";
        lblNumberOfRafts.Text = "";
        lblTotalGuideFee.Text = "";
        lblTotalWeekendSurcharge.Text = "";
        lblSubtotal.Text = "";
        lblSalesTaxCharge.Text = "";
        lblTotalCharge.Text = "";
        ddlTour.SelectedIndex = -1;
        rblDay.SelectedIndex = -1;
        chkGuided.Checked = false;
        //Set focus back to name textbox
        txtNumberOfRafters.Focus();
    }
}

如果返回错误页面,c#就会出错

让我印象深刻的是Convert.ToDecimal(…)调用。

        baseTourRate = Convert.ToDecimal(riverTour);
        if (riverTour == "FB")
        {
            baseTourRate = FRENCH_BROAD; 
        }
        else if (riverTour == "NH")
        {
            baseTourRate = NANTAHALA;
        }
        else if (riverTour == "TK")
        {
            baseTourRate = TUCK;
        }

您正在尝试将字符串转换为小数,但随后将该字符串与字符串进行比较。我的猜测是,riverTour并不是一个小数,因此你会得到一个异常。基本上,这个逻辑没有意义。转换。如果不是有效的小数,ToDecimal将抛出异常。因此,riverTour不能包含可转换为十进制的值和可与FB/NH/TK比较的值。你到底想要完成什么?如果你想转换成十进制,如果它是有效的,然后检查这三种情况,如果它不是,那么你应该看看十进制。TryParse(字符串,输出十进制)https://msdn.microsoft.com/en-us/library/9zbda557(v=vs.110).aspx。这可能会帮助你实现你想要做的事情。

如果输入总是数字,那么您应该考虑使用NumericUpDown。或者,至少使用TryParse方法,这样您就可以判断输入是否有效。

numberOfRafters = Convert.ToInt32(txtNumberOfRafters.Text);

也是不安全的