加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无效的投射日期时间?

发布时间:2020-12-16 00:19:49 所属栏目:百科 来源:网络整理
导读:我有一个包含以下代码的类 public cCase(string pCaseNo,string pMode) { if (pMode == "new") { this._caseNo = Validate_CaseNo(pCaseNo); } if (pMode == "existing") { try { int intValidatedCaseNo = Validate_CaseNo(pCaseNo); string sqlText = "SEL
我有一个包含以下代码的类

public cCase(string pCaseNo,string pMode)
    {
        if (pMode == "new")
        {
            this._caseNo = Validate_CaseNo(pCaseNo);
        }
        if (pMode == "existing")
        {
            try
            {
                int intValidatedCaseNo = Validate_CaseNo(pCaseNo);
                string sqlText = "SELECT * FROM tblCases WHERE CaseNo = @CaseNo;";
                string strConnection = cConnectionString.BuildConnectionString();
                SqlConnection linkToDB = new SqlConnection(strConnection);
                linkToDB.Open();
                SqlCommand sqlCom = new SqlCommand(sqlText,linkToDB);
                sqlCom.Parameters.Add("@CaseNo",SqlDbType.Int);
                sqlCom.Parameters["@CaseNo"].Value = intValidatedCaseNo;
                SqlDataReader caseReader = sqlCom.ExecuteReader();
                if (caseReader.HasRows)
                    while (caseReader.Read())
                    {
                        this._claimant = caseReader["Claimant"].ToString();
                        this._defendant = caseReader["Defendant"].ToString();
                        this._caseType = caseReader["CaseType"].ToString();
                        this._occupation = caseReader["Occupation"].ToString();
                        this._doa = (DateTime?)caseReader["DOA"];
                        this._dateClosed = (DateTime?)caseReader["DateClosed"];
                        this._dateSettled = (DateTime?)caseReader["DateSettled"];
                        this._dateInstructed = (DateTime?)caseReader["DateInstructed"];
                        this._status = caseReader["Status"].ToString();
                        this._instructionType = caseReader["InstructionType"].ToString();
                        this._feeEstimate = (decimal?)caseReader["FeeEstimate"];
                        this._amountClaimed = (decimal?)caseReader["AmountClaimed"];
                        this._amountSettled = (decimal?)caseReader["AmountSettled"];
                        this._caseManager = caseReader["CaseManager"].ToString();
                    }
                caseReader.Close();
                linkToDB.Close();
                linkToDB.Dispose();
            }
            catch (Exception eX)
            {
                throw new Exception("Error finding case" + Environment.NewLine + eX.Message);
            }
        }
    }

但是Datetime?强制转换失败并显示“无效投射”.
我检查了SQL数据库,该字段存储了有效日期
所以我无法解决为什么,当我通过DataReader将信息提取到我的应用程序时,datetime字段导致Invalid Cast.

请帮忙.

解决方法

您将要更改以下行:

this._doa = (DateTime?)caseReader["DOA"];

至:

if (caseReader["DOA"] != DBNull.Value)
    this._doa.Value = (DateTime)caseReader["DOA"];

以及所有类似的线条.

DBNull值无法从Nullable类型中转换.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读