Click here to Skip to main content
13,804,051 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i want that if 9:00 > 8:00 then in result will be 1 in third column

What I have tried:

DateTime dt1 = Convert.ToDateTime(txtTime.Text.Trim()).Date;
           DateTime dt2 = Convert.ToDateTime(txtLTime.Text.Trim()).Date;

           if (int.Parse(txtLTime.Text) > int.Parse(txtTime.Text))
               {
                   txtLate.Text = "1";
               }
Posted
Updated 8-Dec-18 4:12am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please, look at your code.
Fist, you convert the user input to DateTime values:
DateTime dt1 = Convert.ToDateTime(txtTime.Text.Trim()).Date;
DateTime dt2 = Convert.ToDateTime(txtLTime.Text.Trim()).Date;
(Though it's a lot better idea to use DateTime.TryParse instead of Convert as it lets you tell the user there was a problem instead of your app crashing).
Then you ignore those and try to convert the same user inputs to integer:
if (int.Parse(txtLTime.Text) > int.Parse(txtTime.Text))
One of those two conversions will fail: if the data is a date, the integer conversion will fail; if the user inputs an integer, the DateTime conversions will fail. Either way, your app will crash.

Use TryParse and compare the results:
DateTime dt1;
if (!DateTime.TryParse(txtTime.Text.Trim(), out dt1))
   {
   ... report problem to user ...
   return;
   }
dt1 = dt1.Date;
DateTime dt2;
if (!DateTime.TryParse(txtLTime.Text.Trim(), out dt2))
   {
   ... report problem to user ...
   return;
   }
dt2 = dt2.Date;
if (dt2 > dt1)
   {
   ...
  Permalink  
Comments
Member 12314309 8-Dec-18 10:40am
   
i did this but not resolved
OriginalGriff 8-Dec-18 10:47am
   
Which tells us nothing - we can't see your screen, so we have no idea what you actually typed in, or what happened when you ran it.
So show us the code fragment, show us what the debugger showed was in the textboxes, and tell us what it did that you didn't expect or didn't do that you did!
Member 12314309 8-Dec-18 10:52am
   
i have debug no error is coming code working fine but result is not coming .i did this code as you told

DateTime dt1;
if (!DateTime.TryParse(txtTime.Text.Trim(), out dt1))
{

return;
}
dt1 = dt1.Date;
DateTime dt2;
if (!DateTime.TryParse(txtLTime.Text.Trim(), out dt2))
{

return;
}
dt2 = dt2.Date;
if (dt1 > dt2)
{
txtLate.Text="1";
}
OriginalGriff 8-Dec-18 11:13am
   
So what happened to the "... report problem to user ..." bit? You don't do that, you just return - so if either of the textboxes fails to parse as a valid DateTime then your code does nothing and you don't know about it, or why.

Which is exactly what you are seeing ...
Member 12314309 8-Dec-18 12:54pm
   
i have resolve my issue using this code
DateTime dt1 = Convert.ToDateTime(txtTime.Text.Trim());
DateTime dt2 = Convert.ToDateTime(txtLTime.Text.Trim());

if (dt1 > dt2)
{
txtLate.Text = "1";
}
OriginalGriff 8-Dec-18 13:42pm
   
So you ask a question, we explain what you need to do and why. And you ignore it in favour of code that will crash your app when the user makes a mistake.

Have fun.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181218.1 | Last Updated 8 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100