Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 84.6k

why does the program not stop?

Apr 17 2021 11:10 AM
Hi
When i leave the field 'begdat' (of a gridview in editing mode) empty or with anything except date , i get the error 'Not a date 1' and i'm compelled to fill a valid date. The error "Not a date 2." at line 16 is never shown.
Now, if i remove lines 13 to 17 and i leave the field empty or with anything except date, line 18 gives this error "String was not recognized as a valid DateTime". This is what i don't understand. Normally, the program would be stopped at line 4. How come that line 4 now is ignored and that the program goes further?
 
Thanks.
  1. protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)  
  2. GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];  
  3. TextBox begdattxt = (TextBox)row.FindControl("begdat");  
  4. if (!DateTime.TryParse(begdattxt.Text,out newDate))  
  5. {  
  6. e.Cancel = true;  
  7. ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript"" alert('Not a date 1.');"true);  
  8. }  
  9. else  
  10. {  
  11. begdat = Convert.ToDateTime(e.NewValues["begdat"]);  
  12. }  
  13. ....  
  14. else if (!DateTime.TryParse(begdattxt.Text, out newDate) || !DateTime.TryParse(enddattxt.Text, out newDate))  
  15. {  
  16. e.Cancel = true;  
  17. ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript"" alert('Not a date 2.');"true);  
  18. }  
  19. else if (Convert.ToDateTime(enddattxt.Text) < Convert.ToDateTime(begdattxt.Text))  
  20. {  
  21. flag = "1";  
  22. e.Cancel = true;  
  23. Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript"" alert('endate must be >= begindate.');"true);  
  24. enddattxt.Focus();  
  25. }  

Answers (2)