简体   繁体   English

在C#中传递Asp.Net控件的DateTime的空值

[英]Passing Null Value of DateTime of Asp.Net control in C#

I have a TextBox control that holds a date, that often could be null . 我有一个保存日期的TextBox控件,该日期通常可以为null I'm using it in an Insert and when I try to insert I'm getting the error message: 我在插入中使用它,当我尝试插入时出现错误消息:

String was not recognized as a valid DateTime.

I'm unsure how to pass the null value as a datetime. 我不确定如何将空值作为日期时间传递。 Here is my code. 这是我的代码。

It's Convert.ToDateTime(txtDateAction.Text) that I'm having problems with. 我遇到问题的是Convert.ToDateTime(txtDateAction.Text)

protected void btnLog_Click(object sender, EventArgs e)
    {

            PhoneLogsBLL.InsertOCASPhoneCall(
            Convert.ToInt16(txtStaffID.Text), 
            Convert.ToDateTime(txtDate.Text),                
            chkAdvisoryBoard.Checked, 
            chkClaims.Checked, 
            chkDR.Checked, 
            chkEEOICPA.Checked, 
            chkOCAS.Checked, 
            chkPhysicianPanel.Checked, 
            chkPC.Checked, 
            chkWebsite.Checked, 
            chkSEC.Checked, 
            chkCATI.Checked, 
            chkNIOSH800.Checked, 
            chkORAU800.Checked, 
            chkCongressional.Checked, 
            chkCOI.Checked, 
            chkDOLOutreach.Checked, 
            txtDiscussion.Text, 
            chkActionRequired.Checked, 
            Convert.ToDateTime(txtDateAction.Text),
            txtAction.Text, 
            0, 
            chkActivityReport.Checked);
    } 

 public static void InsertOCASPhoneCall(
        short sintStaffID,
        DateTime dtmDateCalled,
        bool blnAB,
        bool blnClaims,
        bool blnDR,
        bool blnEEOICPA,
        bool blnOCAS,
        bool blnPhysicianPanel,
        bool blnPC,
        bool blnWebSite,
        bool blnSEC,
        bool blnCATI,
        bool blnNIOSH800,
        bool blnORAU800,
        bool blnCC,
        bool blnCOI,
        bool blnDOL,
        string vcharDiscussion,
        bool blnActionNeeded,
        DateTime dtmActionTaken,
        string ActionTaken,
        short sintActionTakenStaffID,            
        bool blnActivityReport)
    {
        UnitOfWorkCTS uow = new UnitOfWorkCTS();

        using (var repo = new OCASPhoneCallsRepository(uow))
        {
            OCASPhoneCalls call = new OCASPhoneCalls();

            call.dtmDateCalled = dtmDateCalled;
            call.sintStaffID = sintStaffID;
            call.blnAB = blnAB;
            call.blnClaims = blnClaims;
            call.blnDR = blnDR;
            call.blnEEOICPA = blnEEOICPA;
            call.blnOCAS = blnOCAS;
            call.blnPhysicianPanel = blnPhysicianPanel;
            call.blnPC = blnPC;
            call.blnWebSite = blnWebSite;
            call.blnSEC = blnSEC;
            call.blnCATI = blnCATI;
            call.blnNIOSH800 = blnNIOSH800;
            call.blnORAU800 = blnORAU800;
            call.blnCC = blnCC;
            call.blnCOI = blnCOI;
            call.blnDOL = blnDOL;
            call.vcharDiscussion = vcharDiscussion;
            call.blnActionNeeded = blnActionNeeded;
            call.dtmActionTaken = dtmActionTaken;
            call.sintActionTakenStaffID = sintActionTakenStaffID;
            call.blnActivityReport = blnActivityReport;

            repo.InsertOrUpdate(call);
            uow.Save();

        }
    }

Your property should be marked as 您的财产应标记为

 DateTime? dtmActionTaken,

You should check if the textbox has text in it first: 您应该先检查textbox是否包含文本:

string.IsNullOrEmpty(txtDateAction.Text) ? (DateTime?)null : Convert.ToDateTime(txtDateAction.Text),

You should be testing the following scenario too: If the textbox has text but is not a valid date, 您也应该测试以下情形:如果textbox包含文本,但不是有效日期,

The DateTime is a struct so you can not directly set it to null. DateTime是一个结构,因此您不能直接将其设置为null。 Try writing a check for the value is null or empty before applying the converter. 在应用转换器之前,请尝试编写一个值为null或为空的检查。

I would probably do the following: 我可能会执行以下操作:

  1. Change the InsertOCASPhoneCall method signature so that the dtmActionTaken parameter is a DateTime? 更改InsertOCASPhoneCall方法签名,以便dtmActionTaken参数为DateTime? (or Nullable<DateTime> , the two are equivalent—the first being syntactic sugar). (或Nullable<DateTime> ,两者是等效的-第一个是语法糖)。

  2. Then, use string.IsNullOrEmpty(txtDateAction.Text) ? 然后,使用string.IsNullOrEmpty(txtDateAction.Text)吗? null : Convert.ToDateTime(txtDateAction.Text) : null. null:Convert.ToDateTime(txtDateAction.Text):null。

May I suggest adding an error catch that if the text value is null set the date as todays date? 我是否可以建议添加一个错误捕获,如果文本值为null,则将日期设置为今天? Because there is no 'null' date value in the common sense (its something like #12:00:00AM#). 因为在一般意义上没有“空”日期值(类似于#12:00:00 AM#)。

Something along the lines of (In VB.net): 类似于(在VB.net中):

If Textbox1.Text = "" then
    Textbox1.Text = Date.Now
Else
     CDate (Textbox1.Text)
End if

And in c#: 在C#中:

if (string.IsNullOrEmpty(Textbox1.Text)) {  Textbox1.Text = System.DateTime.Now; } else {   Convert.ToDateTime(Textbox1.Text); }

Since you are passing a DateTime to InsertOCASPhoneCall, and not a Nullable, one must assume it is required. 由于您将DateTime传递给InsertOCASPhoneCall而不是Nullable,因此必须假定它是必需的。 As such, you should put an if statement around the call to InsertOCASPhoneCall to make sure it has a valid value: 这样,您应该在对InsertOCASPhoneCall的调用周围放置一个if语句,以确保它具有有效值:

DateTime dtmActionTaken;
if (DateTime.Parse(txtDateAction.Text, out dtmActionTaken) == false)
{
    // notify the user of the invalid date
}
else
    PhoneLogsBLL.InsertOCASPhoneCall(
        Convert.ToInt16(txtStaffID.Text), ...
        ...dtmActionTaken

If the database can indeed handle a NULL for the ActionTaken column, then you can instead change the dtmActionTaken parameter to InsertOCASPhoneCall to DateTime? 如果数据库确实可以为ActionTaken列处理NULL,则可以改为将dtmActionTaken参数的InsertOCASPhoneCall更改为DateTime? dtmActionTaken, and pass in null when it does not parse. dtmActionTaken,如果不解析则传入null。 Something like: 就像是:

DateTime dtmActionTaken;
DateTime? dtmActionTakenParm = null;

if (DateTime.Parse(txtDateAction.Text, out dtmActionTaken) == false)
{
    dtmActionTakenParm = dtmActionTaken;
}

PhoneLogsBLL.InsertOCASPhoneCall(
        Convert.ToInt16(txtStaffID.Text), ...
        ...dtmActionTakenParm

Although you still may want to notify the user if it does not parse (as opposed to being an empty string) 尽管您仍可能希望通知用户是否未解析(而不是为空字符串)

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

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