Click here to Skip to main content
11,412,516 members (74,770 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have tried this code but its always giving me ouptut as afternoon please check it and tell me where i go wrong....
if (DateTime.Now.Hour >= 00 && DateTime.Now.Hour <= 11.59)
               {
                   query = "UPDATE Registration_Master SET session= 'MORNING' WHERE regCode= '" + code + "'";
                   com = new SqlCommand(query, con);
                   com.CommandType = CommandType.Text;
                   com.ExecuteNonQuery();
               }
               else if (DateTime.Now.Hour >= 1 && DateTime.Now.Hour <= 17)
               {
                   query = "UPDATE Registration_Master SET session= 'AFTERNOON' WHERE regCode= '" + code + "'";
                   com = new SqlCommand(query, con);
                   com.CommandType = CommandType.Text;
                   com.ExecuteNonQuery();
               }
               else if (DateTime.Now.Hour >= 18 && DateTime.Now.Hour <= 19)
               {
                   query = "UPDATE Registration_Master SET session= 'EVENING' WHERE regCode= '" + code + "'";
                   com = new SqlCommand(query, con);
                   com.CommandType = CommandType.Text;
                   com.ExecuteNonQuery();
               }
               else if (DateTime.Now.Hour >= 20 && DateTime.Now.Hour <= 24)
               {
                   query = "UPDATE Registration_Master SET session= 'NIGHT' WHERE regCode= '" + code + "'";
                   com = new SqlCommand(query, con);
                   com.CommandType = CommandType.Text;
                   com.ExecuteNonQuery();
               }
Posted 1-Nov-12 21:00pm
Edited 1-Nov-12 21:04pm
Mehdi Gholam270.2K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try (keep the 24hr format 1pm -> 13):
else if (DateTime.Now.Hour >= 13 && DateTime.Now.Hour <= 17)
  Permalink  
Comments
Milind Thakkar at 2-Nov-12 2:18am
   
Yup. I think thats the problem...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

DateTime.Now.Hour is Integer value, and it varies from 0 to 23... try the code below:
  
string session = "";
  if (DateTime.Now.Hour >= 6 && DateTime.Now.Hour < 12)
      {
         session = "Morning";
      }
      else if (DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 18)
      {
         session = "Afternoon";
      }
      else if (DateTime.Now.Hour >= 18 && DateTime.Now.Hour < 20)
      {
         session = "Evening";
      }
      else
      {
         session = "Night";
      }
 
       query = "UPDATE Registration_Master SET session= '" + session +"' WHERE regCode= '" + code + "'";
       com = new SqlCommand(query, con);
       com.CommandType = CommandType.Text;
       com.ExecuteNonQuery();
  Permalink  
v3
Comments
Madhuri Patil at 2-Nov-12 2:27am
   
this is good one,and i think this will be give proper answer.
Kuthuparakkal at 2-Nov-12 2:31am
   
thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Morning
00 to 12
AfterNoon
12.01 to 17
Evening
17.01 to 19
Night
19.01 to 24

Please change if condition and try this..
  Permalink  
Comments
Kuthuparakkal at 2-Nov-12 2:31am
   
Can not have values like 12.01 or 17.01 for an int32 datatype, it's a fallacy

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

  Print Answers RSS
0 OriginalGriff 384
1 Sascha Lefévre 200
2 Maciej Los 155
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,247
2 Maciej Los 3,525
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150427.1 | Last Updated 2 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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