2017-03-19 185 views
-1

我在爲一個賦值工作掙扎,試圖讓我的代碼檢查結束日期必須比C#中ASP.NET中的開始日期更晚。老師說:日期檢查器的IF語句ASP.NET

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)

這是我有:

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

我收到以下錯誤:

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. 

任何幫助將不勝感激。 米格爾

+1

聽起來像是你的錯誤是在調用'Parse',而不是比較。請確保在Request [「txtStartDate」]和Request [「txtEndDate」]中檢查字符串,並查看Parse是否知道如何將它們解析爲DateTime對象。 – yeedle

+0

無法得到比這更清晰 - >「附加信息:字符串未被識別爲有效的日期時間。」但是yeyeyeedle已經提到你應該注意什麼。 –

+0

你應該在你的問題中增加你在textStartDate和txtEndDate中傳遞的值。之後,我相信我們可以指示您如何正確解析日期時間。 – vgwizardx

回答