?
Solved

Nested IF statement help

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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