2017-03-19 1 views
-1

私は割り当てを苦労して、終了日がASP.NETのC#の開始日より後でなければならないことを確認するようにしました。先生は言います:日付チェッカーのIF文

Create an If statement to check that both start and end date exist as COMPOUND condition. Hint: use &&. Inside this if statement (check date-difference)

は、ここで私が持っているものだ。すべてのヘルプは大歓迎されます

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code. Additional information: String was not recognized as a valid DateTime. 

DateTime startDate = DateTime.Parse(Request["txtStartDate"]); 
    DateTime endDate = DateTime.Parse(Request["txtEndDate"]); 
    if (DateTime.Compare(startDate, endDate) > 0) 
    { 
     txtStartDate.BackColor = System.Drawing.Color.Yellow; 
     txtEndDate.BackColor = System.Drawing.Color.Yellow; 
     lblError.Text += "The end date must be a later date than the start date."; 
     //The Msg text will be displayed in lblError.Text after all the error messages are concatenated 
     bIsFormValid = false; 
     //Boolean value - test each textbox to see if the data entered is valid, if not set validState=false. 
     //If after testing each validation rule, the validatedState value is true, then submit to frmPersonnelVerified.aspx, if not, then display error message 
    } 
    else 
    { 
     txtStartDate.BackColor = System.Drawing.Color.White; 
     txtEndDate.BackColor = System.Drawing.Color.White; 
    } 


    // Redirect the User to frmStudentConfirmed.aspx 
    if (bIsFormValid) 
    { 
     Response.Redirect("frmPersonnelVerified.aspx"); 
    } 
    else 
    { 
     Session.Clear(); 
    } 

は、私は次のエラーを取得しています。 ミゲル

+1

が鳴ります。 'Request [" txtStartDate "]'と 'Request [" txtEndDate "]'の文字列をチェックして、それらを解析してDateTimeオブジェクトに変換する方法を知っているかどうか確認してください。 – yeedle

+0

はこれ以上明らかにすることはできません - > "追加情報:文字列が有効なDateTimeとして認識されませんでした。"しかし、ええ@yeedleは、あなたが目を向けなければならないことについて言及しています。 –

+0

textStartDateとtxtEndDateに渡す値を質問に追加する必要があります。その後、私はdatetimeを正しく解析する方法を教えてくれるはずです。 – vgwizardx

答えて

0

問題は、あなたが、デフォルトではフォーマットの数を受け入れる

Request["txtStartDate"] 

または

Request["txtEndDate"] 

DateTime.Prase静的メソッドに渡ししようとしている日付の形式であるように思えますあなたが別の形式で日付を渡す必要がある場合は、 https://www.dotnetperls.com/datetime-parse

あなたのエラーがないと比較して、Parse` `への呼び出しであるよう

DateTime.ParseExact https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

+0

ありがとうございます!完璧に働いた! –

関連する問題