It's been a long while since I've last been here but I've spent the last 2 hours pulling my hair out trying to figure this out and I'm hoping for a bit of help with this one.

I have a question/response check (a human verification check) on my page and it refuses to work... I've written the code such that it generates the question and answer on Page_Load only on the first (non postback) load. I've generated a label which has it's text property pulled from the Question as generated on Page_Load and I'm trying to validate the answer from the generated answer from Page_Load. For some reason it always validates as false even if I put the answer straight from the expected answer into the text box I'm validating from.

Question/Answer Generation:

private string hQuest;
    private string hAns;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            humQChange();
        }
    }

    private void humQChange()
    {
        humScript regHum = new humScript();
        hQuest = regHum.getHQuestion();
        hAns = regHum.getHAnswer();
        hQuestionLabel.Text = hQuest;
    }

Validation:

private bool checkHumResp()
    {
        if (hAnswer.Text == hAns)
        {
            return true;
        }
        else
        {
            testLabel.Text = "Failed";
            return false;
        }
    }

Even if I put a line of code in that tells it to fill hAnswer.Text with the value from hAns I still get a failure when validating one against the other. Both are string based variables so this isn't making any kind of sense to me.

Any suggestions?

First thing to do would be to debug the code at the line comparing the two. Visually confirm they are the same.

commented: Thanks for at least trying :) Nobody else seems to have any thoughts on this one. +9

Well, as I said, if I insert a line that says hAnswer.Text = hAns it then places the value into hAnswer... following that, the comparison of hAnswer.Text to hAns should match but it still doesn't for some reason.

Even though hAnswer.Text and hAns is the same on the first trip, you would still want to do a walk through of the code to make sure that the value is not changing when the page is posted back.

Have you tried debugging it to make sure the logic is flowing correctly?

commented: Agree with you and Hearth, I'll have to look into that this week to see if it's the case... +9

It is possible that the page variable hAns is losing its value on postBack. You could try storing this in a Session or Viewstate variable instead.

However, as the other guys have suggested, you should place a breakpoint on the line of code for the comparison and check what value each has at that time. Making the assumption that a variable/object has a value because you have set a value at some previous time is not necessarily valid.

commented: Agree with you and Consider, I'll have to look into that this week to see if it's the case... +9

You know... I never thought about that... the thing I've been doing is generating (for testing sake) the answer into the hAnswer.Text at the point it's generated. It never occurred to me that during the postback of the page it might clear the variable...

I always kind of figured the point of a locally stored variable was to store info between states lol

Oh snap! I forgot to close this.

Yeah the issue was me being out of practice and completely forgetting that, while a C# program will carry forward variables from state to state, a web-based asp.net script will not carry said variables to the next page state without storing them in some way between states.

Being lazy, I'll just store the "answer" in an invisible field on the page as it's only a "Human Verification" deal and not of any grave security concern.

Thanks again for the help folks :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.