简体   繁体   English

可以为空的 object 在传递存储过程时必须有一个值

[英]Nullable object must have a value while pass in stored procedure

I am trying to send ClearDateC as parameter to procedure but it throwing an error我正在尝试将ClearDateC作为参数发送给过程,但它会引发错误

nullable object must have value.可为空的 object 必须有值。

My code:我的代码:

foreach (var item in bankRecos)
{
   DataTable Dt ;

   DateTime ChqDateC = Convert.ToDateTime(item.ChqdateDisplay);

   Dt = MD.GetBankReco_GetBankRecoUpdate(item.CommentC, (DateTime)item.ClearDateC, item.Chqno, ChqDateC);

   if (Dt.Rows.Count > 0 && Dt.Rows[0]["Status"].ToString() == "1")
   {
       status = "Success";
   }
   else
   {
       status = "Error : " + Dt.Rows[0]["Message"].ToString().Replace('\n', '_');
   }
}

Please help me with this.请帮我解决一下这个。

You can change the call to check if the nullable DateTime filed has value and then use the Value property like您可以更改调用以检查可为空的 DateTime 字段是否具有值,然后使用Value属性,例如

var cleardate = item.ClearDateC.HasValue ? (DateTime)item.ClearDateC : DateTime.Now;
MD.GetBankReco_GetBankRecoUpdate(item.CommentC, cleardate, item.Chqno, ChqDateC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM