Solved

Nested IF statement help

Posted on 2011-02-14
3
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Expert Comment

by:shrikantss
ID: 34890509
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
ID: 34890686
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
ID: 34961902
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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