I'm attempting to write a try/catch, and failing obviously. I'm not sure I fully understand try/catch, but I do know that I'm having troubles wrapping my head around what I would need to happen in order to verify that a correct date is being entered submit via form. I mean, I gather it needs to be in DateTime format, not string, and should be (MM/dd/yyyy), but this try/check thing is throwing me for a loop.
Instructions: Go back to the validation code that you added in the frmPersonnel code and add a try/catch with logic to prevent an invalid date from causing a server error.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class frmPersonnel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.Click += new EventHandler(this.btnSubmit_Click);//event for button
}
private void btnSubmit_Click(object sender, EventArgs e)
{
//DECLARATIONS
int count = 0;
string Msg = "You must enter a value in the following fields: <br/> ";
Boolean validatedState = true;
Boolean validateEntry = false;
DateTime endDate = new DateTime(2016, 03, 01);
DateTime startDate = new DateTime(2016, 03, 01);
//BEGIN SERIES OF IF/ELSE FOR CONFIRMING ENTRIES
if (Request["txtFirstName"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtFirstName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "First Name <br/> ";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtLastName"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtLastName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Last Name <br/> ";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtPayRate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtPayRate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Pay Rate <br/> ";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtStartDate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtStartDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg + "Start Date <br/> ";
}//endif
else
{
try
{
//Conversion to DateTime format?
startDate = DateTime.Parse(Request["txtStartDate"]);
//How do I write the format I want, and when it should be checked?
}
catch (Exception ex)
{
//Exception should be caught here, not sure how to write this out though?
}
validateEntry = true;
}//end else
if (Request["txtEndDate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtEndDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg + "End Date <br/> ";
}//endif
else
{
try
{
//Conversion to DateTime format?
endDate = DateTime.Parse(Request["txtEndDate"]);
//How do I write the format I want, and when it should be checked?
}
catch (Exception ex)
{
//Exception should be caught here, not sure how to write this out though?
}
validateEntry = true;
}//end else
//END SERIES OF IF/ELSE FOR CONFIRMING ENTRIES
//START IF VALIDATE ENTRY
if (validateEntry == true)
{
if (DateTime.Compare(startDate, endDate) >= 0)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Start Date <br/>" + "End Date <br/> <br/>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
validatedState = 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 //goes to this if dates are correct
{
validatedState = true;
count += 2;
txtStartDate.BackColor = System.Drawing.Color.White;
txtEndDate.BackColor = System.Drawing.Color.White;
}
}
//END IF VALIDATE ENTRY
//SENDS DATA & ERROR MESSAGES
if (count == 5 && validatedState == true)
{
Session["txtFirstName"] = txtFirstName.Text;
Session["txtLastName"] = txtLastName.Text;
Session["txtPayRate"] = txtPayRate.Text;
Session["txtStartDate"] = txtStartDate.Text;
Session["txtEndDate"] = txtEndDate.Text;
Response.Redirect("frmPersonnelVerified.aspx");
//sends to other page
}
else
{
//Writes out error messages
Response.Write("<br/><span style = 'color: red ; position: absolute; top: 360px; left: 90px;'>" + Msg + "</span>");
}
//ENDS DATA AND ERROR MESSAGES
}//end Function: private void BtnSubmit_click...
}