Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i want to compare the value in textbox(Time) with the value(Time also) in my database, but somehow it would not compare the value, it would always give me the MessageBox.Show("Your date and time is valid.");, any help?

C#
private void onShowClick(object sender, RoutedEventArgs e) {

           SqlConnection conn;
           SqlCommand cmdSelect;
           SqlDataReader reader;
           string connStr = ConfigurationManager.ConnectionStrings["house"].ConnectionString;
           conn = new SqlConnection(connStr);

           string select = "Select Date, Time, EndTime from Schedule";
           cmdSelect = new SqlCommand(select, conn);

           try {
               conn.Open();
               reader = cmdSelect.ExecuteReader();
               var valueDate = "";
               var valueStart = "";


               while(reader.Read()){

                   valueDate = reader["Date"].ToString();
                   valueStart = reader["Time"].ToString();

               }

               if (datePicker1.SelectedDate.ToString().Equals(valueDate) && timeText.Text.Equals(valueStart))
               {
                      MessageBox.Show("You have a same date and same time in your record(s). Please choose another date and/or time");
               }
               else {
                   MessageBox.Show("Your date and time is valid.");
               }
               conn.Close();
           }
           catch(Exception ex){
               MessageBox.Show(ex.Message);
           }


       }
Posted

Well, yes...it probably will, because it is always comparing the same value - the last one read from your DB.

You don't try to restrict which records are returned with an SQL WHERE clause, so it will return all records. You then loop through them all retrieving and discarding them util the have processed teh last row. The values from that row and that row alone at left in the valueDate and valueStart as strings.

Then you check that one single value against the user selected date, and report accordingly.

I suspect that you either want to move your test into the loop, or (better) use a WHERE clause to return only those values (or even better yet a count of those values) which do match.

BTW: You aren't storing your dates as strings in the DB are you? Because it looks like you are and that is a very, very poor idea that leads to loads of problems later. A DATETIME field is a lot more useful...
 
Share this answer
 
Apparently, the date and time returned from the database are not the same as those presented in the datapicker and the textbox.
You can help yourself by doing a debug, use the Debug.Write method to check the values of the data and time returned from the database against those from the user.
Check this out on how to use Debug.Write [^]
One of the possible reason for these discrepancies could be the different date and time format used in the database.
 
Share this answer
 

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