3

I have a TextBox control that holds a date, that often could be 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.

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();

        }
    }
1

5 Answers 5

15

Your property should be marked as

 DateTime? dtmActionTaken,

You should check if the textbox has text in it first:

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,

Sign up to request clarification or add additional context in comments.

4 Comments

on this part null : Convert.ToDateTime(txtDateAction.Text) I'm getting an error message Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'
You need to cast that null: (DateTime?)null or default(DateTime?)
@hollyquinn I have updated the answer as per Hans Kesting's suggestion.
@Seany84 Good catch on casting null to DateTime?.
0

The DateTime is a struct so you can not directly set it to null. Try writing a check for the value is null or empty before applying the converter.

1 Comment

Well, Nullable<T> is also a struct, but you can set that to null... the problem is that DateTime is a non-nullable value type.
0

I would probably do the following:

  1. Change the InsertOCASPhoneCall method signature so that the dtmActionTaken parameter is a DateTime? (or Nullable<DateTime>, the two are equivalent—the first being syntactic sugar).

  2. Then, use string.IsNullOrEmpty(txtDateAction.Text) ? null : Convert.ToDateTime(txtDateAction.Text) : null.

3 Comments

Isn't this just what I have written beforehand: stackoverflow.com/a/22689567/550198
I'm getting the same error as I am with Seany84's code.
@Seany84 Yes, but I was already writing my answer and posted it anyway.
0

May I suggest adding an error catch that if the text value is null set the date as todays date? Because there is no 'null' date value in the common sense (its something like #12:00:00AM#).

Something along the lines of (In VB.net):

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

And in c#:

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

Comments

0

Since you are passing a DateTime to InsertOCASPhoneCall, and not a Nullable, one must assume it is required. As such, you should put an if statement around the call to InsertOCASPhoneCall to make sure it has a valid value:

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? dtmActionTaken, and pass in null when it does not parse. 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.