Below is the code of my simple page’s submit button click. I am submitting an empty form but no server validation error messages are showing. What’s wrong with my code? When I click on submit, the page just turns blank and nothing happens. I am also unable to attach the debugger.
When I am building my website project, it is not showing any compilation error either. I don’t know what I am doing wrong.
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Need to Validate All Required Fields before redirecting to frmPersonalVerified.aspx
bool blnFormIsValid = true;
DateTime dtEndDate;
DateTime dtStartDate;
// Get Date because we have a value.
dtEndDate = DateTime.Parse(txtEndDate.Text);
// Get Date because we have a value.
dtStartDate = DateTime.Parse(txtStartDate.Text);
if (txtFirstName.Text.Trim() == "")
{
txtFirstName.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please enter first name.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtFirstName.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
if (txtLastName.Text.Trim() == "")
{
txtLastName.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please enter last name.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtLastName.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
if (txtPayRate.Text.Trim() == "")
{
txtPayRate.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please enter pay rate.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtPayRate.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
if (txtStartDate.Text.Trim() == "")
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please enter start date.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtStartDate.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
if (txtEndDate.Text.Trim() == "")
{
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please enter end date.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtEndDate.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
// Compare Dates
if (DateTime.Compare(dtStartDate, dtEndDate) >= 0)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text = "Please make sure that start date is less than end date.";
blnFormIsValid = false;
}
else
{
lblError.Text = "";
txtStartDate.BackColor = System.Drawing.Color.White;
txtEndDate.BackColor = System.Drawing.Color.White;
blnFormIsValid = true;
}
if (blnFormIsValid == true)
{
//Assign a value to the session variable.
Session["FirstName"] = txtFirstName.Text;
Session["LastName"] = txtLastName.Text;
Session["PayRate"] = txtPayRate.Text;
Session["StartDate"] = txtStartDate.Text;
Session["EndDate"] = txtEndDate.Text;
// Sends A Request from the Browser to the server.
Response.Redirect("frmPersonalVerified.aspx");
}
}
Update
I just used .Equals(“”)… it’s not working. Still blank page is showing up
Ignoring the fact that the correct way is to use ASP.NET’s built-in validation tools, the problem is that your program’s logic is broken.
You’re using
blnFormIsValidto store the validity of the form, however its value is meaningless because you’re assigning it without paying attention to previous state.If I submit your page’s form with these values…
…then it will correctly fail the first validation and
blnFormIsValidwill be false, however your next check ignores the state ofblnFormIsValidand sets it totruesimply because txtLastName’s value is valid.This issue stems not from our lack of understanding or knowledge of ASP.NET, but basic programming and logic. A simple step-through or dry-run of your code would have revealed this.
Below are my list of recommendations:
Use ASP.NET Validation controls, like so:
Don’t use Hungarian notation
This is when you prefix an identifier with a tag that identifies its type, e.g. “blnFormIsValid” or “txtFirstName”. Just use “formIsValid” or “firstName”. Hungarian notation is only of use in environments where typing information is not provided by the editor.
Don’t use
foo == true… because the operation will evaluate to the same value as
foo. In your case you should haveif( formIsValid )instead ofif( formIsValid == true ). Avoiding unnecessary use of the==operator can help avoid cases where you accidentally use the=assignment operator instead of the==equality operator (and make your code more readable).