输入字符串的格式不正确

时间:2013-04-19 05:38:40

标签: sql string winforms format

我有一个课程,我在其中计算剩余学分。并通过DataTable返回值。但是在IF条件下它会抛出错误'输入字符串格式不正确。',代码中的错误。

请帮助, 在此先感谢

        string cmd2 = "select sum(FacilityAmountINR),sum(LCAmountPaid) from FacilityIssueDetails where Status='open' AND LCType = 'Inland LC'";
        DataTable a = DbRdRw.SqlDbRead(cmd2, "FacilityIssueDetails");
        decimal AvailInlandLC = 0;
        string totcred = Convert.ToString(a.Rows[0]["Column1"]);
        string totpaid = Convert.ToString(a.Rows[0]["Column2"]);
        if (totcred != null && totpaid != null)
        {
            decimal TotalAmountCredit = Convert.ToDecimal(totcred);// error here
            decimal TotalAmountpaid = Convert.ToDecimal(totpaid); // error here
            AvailInlandLC = InlandLC - TotalAmountCredit + TotalAmountpaid;
        }
        //*************************************************************************
        DataTable AvailableLimitTable = new DataTable();
        AvailableLimitTable.Columns.Add("LCType",typeof (string));
        AvailableLimitTable.Columns.Add("TotalLimit",typeof (decimal));
        AvailableLimitTable.Columns.Add("AvailableLimit",typeof (decimal));

        DataRow dr = AvailableLimitTable.NewRow();
        dr["LCType"] = "Inland LC";
        dr["TotalLimit"] = InlandLC;
        dr["AvailableLimit"] = AvailInlandLC;
        AvailableLimitTable.Rows.InsertAt(dr, 0);

1 个答案:

答案 0 :(得分:0)

如果从数据库中获取NULL值,请尝试更改if循环条件...

if (totcred != null && totpaid != null && totcred != "Null" && totpaid != "Null" )
        {
            decimal TotalAmountCredit = Convert.ToDecimal(totcred);// error here
            decimal TotalAmountpaid = Convert.ToDecimal(totpaid); // error here
            AvailInlandLC = InlandLC - TotalAmountCredit + TotalAmountpaid;
        }