Solved

Nested IF statement help

Posted on 2011-02-14
3
608 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 71
Run time Error 4 47
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
Release Dynamically Allocated Memory in C# 3 25
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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