如何解决asp.net中的格式异常错误?

时间:2013-05-13 05:09:40

标签: c# asp.net formatexception

我在调试时收到此错误。

  

将字符串转换为日期时对象解析要采用的字符串   将每个变量放入日期时间对象之前的日期

任何人都可以建议我解决方法吗?

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        var employeeid = PayrollHelper.Context.Payroll_PersonalDetails.Where(a => a.Employee_Code == txtEmployCode.Text).Select(a => a.Employee_id).First();
        var source = PayrollHelper.Context.Payroll_SalaryDetails.Where(a => a.fkEmployee_ID == Convert.ToInt32(employeeid) && a.SalaryOfMonth.Value.Month == Convert.ToDateTime(lbMonth.Text).Month).Select(a => a);

        if (source.Count() == 0)
        {
            var Count = PayrollHelper.Context.Payroll_AdvanceManagements.Where(a => a.fkEmployee_ID == employeeid).Select(a => a);
            if (Count.Count() != 0)
            {
                var salary_ad = PayrollHelper.Context.Payroll_AdvanceManagements.Where(a => a.fkEmployee_ID == employeeid).Select(a => a).First();
                if ((salary_ad.Salary_Advance > salary_ad.Refund_Advance) && txtAdvanceDeduction.Text != "")
                {
                    salary_ad.Refund_Advance = salary_ad.Refund_Advance + Convert.ToDecimal(txtAdvanceDeduction.Text);
                    PayrollHelper.Context.SubmitChanges();
                    Payroll_AdvanceRefundBL obj = new Payroll_AdvanceRefundBL(Convert.ToDecimal(txtAdvanceDeduction.Text), DateTime.Now, employeeid);

                    if (obj.Insert())
                    {

                    }
                }
            }

            bool cl = false;
            if (ViewState["cl"] != "")
            {
                cl = true;
            }
            Payroll_SalaryDetailBL obj1 = new Payroll_SalaryDetailBL(Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbPay.Text), 2)), Convert.ToSingle(Math.Round(Convert.ToDouble(txtGeneralWorkingDays.Text), 2)), Convert.ToSingle(Math.Round(Convert.ToDouble(lbNumberDay.Text), 2)), Convert.ToDecimal(lbInsentive.Text), Convert.ToString(Math.Round(Convert.ToDecimal(lbExtraHour.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbBenifit.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(txtInvolument.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbGross.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbBasic.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbHRA.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbCridtTotal.Text), 2)), Convert.ToDecimal(Math.Ceiling(Convert.ToDouble(lbNett.Text))), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbSalaryAd.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(txtAdvanceDeduction.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbPF.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbESI.Text), 2)), Convert.ToDecimal(Math.Round(Convert.ToDecimal(lbDeductionTotal.Text), 2)), Convert.ToDateTime(txtSalaryDate.Text).AddMonths(-1), cl, Convert.ToInt32(employeeid));
            if (obj1.Insert())
            {

            }
        }


}

2 个答案:

答案 0 :(得分:1)

该错误意味着您转换为DateTime失败。在这一行:

var source = PayrollHelper.Context.Payroll_SalaryDetails.Where(a => a.fkEmployee_ID == Convert.ToInt32(employeeid) && a.SalaryOfMonth.Value.Month == Convert.ToDateTime(lbMonth.Text).Month).Select(a => a);

具体来说:

Convert.ToDateTime(lbMonth.Text)

在您的代码中,您似乎只想将一个月转换为完整的DateTime。这将抛出FormatException,因为它不是完整的Date或Time。

您正在尝试将其与a.SalaryOfMonth.Value.Month进行比较。无论数据类型是“月”,您都希望做一些事情以不同的方式将“lbMonth.Text”转换为可比较的值。

答案 1 :(得分:1)

lbMonth.Text有什么价值?

如果你得到像1月,2月那样的字符串,那么你需要解析日期时间。

为此只需更换 -

Convert.ToDateTime(lbMonth.Text).Month

DateTime.ParseExact(lbMonth.Text, "MMMM", CultureInfo.CurrentCulture).Month