Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have tried this code but its always giving me ouptut as afternoon please check it and tell me where i go wrong....
C#
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
Updated 1-Nov-12 20:04pm
v2

Try (keep the 24hr format 1pm -> 13):
C#
else if (DateTime.Now.Hour >= 13 && DateTime.Now.Hour <= 17)
 
Share this answer
 
Comments
MT_ 2-Nov-12 2:18am    
Yup. I think thats the problem...
DateTime.Now.Hour is Integer value, and it varies from 0 to 23... try the code below:
C#
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();
 
Share this answer
 
v3
Comments
madhuri@mumbai 2-Nov-12 2:27am    
this is good one,and i think this will be give proper answer.
Kuthuparakkal 2-Nov-12 2:31am    
thanks!
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..
 
Share this answer
 
Comments
Kuthuparakkal 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900