Solved

Nested IF statement help

Posted on 2011-02-14
3
606 Views
Last Modified: 2012-05-11
Under my button click event, I have several if statements. I'm having trouble with the evaluation of the passwordLastSet. Even if it has been more than 24 hours, it goes directly to the else statement that the password has been set less than 24 hours ago.

Cheers,
Brendan
protected void btnSubmit_Click(object sender, EventArgs e)
        {
            System.Guid guid = Guid.NewGuid();
            logInfo.IPaddress = "need to add IP address method";
            if (txtUser.Text != string.Empty)
            {
                string username = txtUser.Text.ToString();

                if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24)
                {
                    if (UserExists(FindName(username)))
                    {
                        logInfo.userName = txtUser.Text.ToString();
                        logInfo.TrackingID = guid;
                        logInfo.requestType = "Request Password";
                        logInfo.email = eMailAddress.ToString();
                        logInfo.InsertRequest();


                        lblStatus.Text = "all is good";

                    }
                    else
                    {
                        lblStatus.Text = "The user does not exists";
                        logInfo.errorType = lblError.Text.ToString();
                        logInfo.userName = txtUser.Text.ToString();
                        logInfo.TrackingID = guid;
                        logInfo.InsertRequestError();
                    }
                    // return a message that the time since last reset has been less than 24 hours
                }
                else
                {
                    lblStatus.Text = "the time has been less than 24 hours";
                }
                    
                
            }
        }

Open in new window

0
Comment
Question by:brendanlefavre
3 Comments
 
LVL 4

Expert Comment

by:shrikantss
Comment Utility
in your code sheck what this line will return

DateTime.Now.Subtract(passwordLastSet).TotalHours
then try to convert that value to integer
by using
 convert.toint16(DateTime.Now.Subtract(passwordLastSet).TotalHours)
then check it
0
 
LVL 7

Accepted Solution

by:
jdavistx earned 500 total points
Comment Utility
Agreed. You should put a breakpoint on that line to see what it's evaluating to.  Also, I would suggest moving the code to some private function, and not to leave it in the event handler.

The below is slightly modified as you weren't using your "username" variable

private void SubmitData()
{
	System.Guid guid = Guid.NewGuid();
	logInfo.IPaddress = "need to add IP address method";
	
	if (!String.IsNullOrEmpty(txtUser.Text))
	{
		string username = txtUser.Text.ToString();

		if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24) //Put a breakpoint on this line
		{
			if (UserExists(FindName(username)))
			{
				logInfo.userName = username;
				logInfo.TrackingID = guid;
				logInfo.requestType = "Request Password";
				logInfo.email = eMailAddress.ToString();
				logInfo.InsertRequest();
				
				lblStatus.Text = "all is good";
			}
		}
		else
		{
			lblStatus.Text = "The user does not exists";
			logInfo.errorType = lblError.Text.ToString();
			logInfo.userName = username;
			logInfo.TrackingID = guid;
			logInfo.InsertRequestError();
		}
		// return a message that the time since last reset has been less than 24 hours
	}
	else
		lblStatus.Text = "the time has been less than 24 hours";
}

protected void btnSubmit_Click(object sender, EventArgs e){ SubmitData(); }

Open in new window

0
 

Author Closing Comment

by:brendanlefavre
Comment Utility
your sample worked, and helped me achieve the result I was looking for. Also I was able to adapt this to a few other areas of my project.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now