We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Confused about a logical paradox

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?

4
Contributors
6
Replies
3 Weeks
Discussion Span
1 Year Ago
Last Updated
10
Views
Question
Answered
Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
Skill Endorsements: 2

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

hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 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.

Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
Skill Endorsements: 2

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?

Consider
Newbie Poster
4 posts since Mar 2012
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

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.

Hearth
Posting Whiz in Training
298 posts since Apr 2008
Reputation Points: 123
Solved Threads: 44
Skill Endorsements: 4

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

Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
Skill Endorsements: 2

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

Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
Skill Endorsements: 2
Question Answered as of 1 Year Ago by hericles, Hearth and Consider

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0968 seconds using 2.73MB