Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Nested IF statement help

Posted on 2011-02-14
3
Medium Priority
?
613 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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