0

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

6
  • 1
    If you're using ASP.NET then why don't you use in-built validators? Commented Sep 23, 2012 at 6:08
  • because I want to learn how to validate server side first..I am kinda a beginner to .net.. Commented Sep 23, 2012 at 6:11
  • You may need to use the Equals() method. Please check this and this. Commented Sep 23, 2012 at 6:16
  • I just used .Equals("")...its not working...still blank page is showing up Commented Sep 23, 2012 at 6:19
  • Does String.Empty work for you? Commented Sep 23, 2012 at 6:23

1 Answer 1

1

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 blnFormIsValid to 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...

txtFirstName = "" // this is invalid
txtLastName = "foo" // this is valid

...then it will correctly fail the first validation and blnFormIsValid will be false, however your next check ignores the state of blnFormIsValid and sets it to true simply 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:

<input type="text" id="firstName" runat="server" />
<asp:RequiredValidator runat="server" controlToValidate="firstName" />

void Page_Load() {
    if( Page.IsPostBack) {
        Page.Validate();
        if( Page.IsValid ) {
            // that's all you have to do
        }
    }

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 have if( formIsValid ) instead of if( 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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.