Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2008, 10:00 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 124
Thanks: 0
Thanked 1 Time in 1 Post
Default Is 9:00 > 08:30 true or false?

I have used a query statement with the following WHERE string to 'Fill' a dataset.

"AND (A.ApptsDate > '" & strApptPreDate & "' OR (A.ApptsDate = '" & strApptPreDate & "' AND A.ApptsTime >= '" & strApptPreTime & "' ))" & _

 and strApptPreTime is defined as:
Dim strApptPreTime As String = myaspx.strPadTime(CStr(dApptCalcNewDate.Hour) + ":" + CStr(dApptCalcNewDate.Minute))


Somehow, the dataset showed only the ApptsTime after 10 am. After more than 2 weeks of debugging, I still can see a dataset watch for > 10 am AppsTime only. Now I am guessting, the problem is the 9 am data in SQL Server database are saved as 9:00, 9:10, etc, while the strPreApptTime is e.g. 08:30. Then I checked the data type settings for these variables. Here are what I found:

In SQL Server Agent job, the ApptsTime data was 'inserted' by @NewApptTimes, which is declared as char(5).
In SQL Server database, the ApptsTime was defined as nvarchar(15).

My question are:
     1. The reason why there were no 9 am data for the dataset, is becasue the WHERE string 9:00 > 08:30 is false?
     2. If the answer to the quation 1 is yes, how do I define AppsTime and/or strApptPreTime, so that 9:00 > 08:30 is true?
     3. I changed the ApptsTime data type to char(5), the SQL Server table still showed 9:00, 9:10, etc., why?

 TIA,
Jeffrey
__________________
C. Jeffrey Wang
 
Old February 1st, 2008, 10:35 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Why are you not storing this data as the appropriate type?? (I.e. DATETIME) It seems like you are having lots of problems with dates and time but you are treating them as strings.

-Peter
 
Old February 1st, 2008, 12:19 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 124
Thanks: 0
Thanked 1 Time in 1 Post
Default

I changed the line for adding "0" into sHourTime.Length=1
to adding " ". The 9 am appointments now can be displayed
in the asspx pages. Now 9:00 > 8:40
 
Old February 1st, 2008, 12:28 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ok... however... what happens when you test this:

 9:30 > 10:00

A text comparison of that will result in true when a datetime comparison will result in false.

You still need to change your data types to work with the appropriate value types otherwise you are going to encounter more problems.

-Peter
 
Old February 2nd, 2008, 12:03 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 124
Thanks: 0
Thanked 1 Time in 1 Post
Default

(A.ApptsDate > '" & strApptPreDate & "' OR (A.ApptsDate = '" & strApptPreDate & "' AND A.ApptsTime >= '" & strApptPreTime & "' ))" & _

9:00 > 8:40 is just an example to demostrate that ApptsTime > strApptPreTime is working now. Previously, at 8:40 am, it was
 9:00 > 08:40 and this Where statement is 'false' so the 9 am appointments data were not display in the dataset table of the aspx page.

At 10:00, of course, 9:00 > 10:00 is false and the 9 am appointments data will be removed from the dataset table.
 
Old February 3rd, 2008, 12:16 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Jeffrey,

Between this and the other thread addressing this issue, I feel like my suggestions have been falling on deaf ears. You have yet to respond to my inquiries as to why you are storing date values as strings instead of the appropriate DateTime data type. Perhaps this sample and explanation will help to convince you that the problems you are having are the result of an incorrect choice in data type usage.

Consider the following code:
Code:
string[] strTimes = new string[] { "8:30", "08:40", "9:00", "10:00" };
for(int i = 0; i < strTimes.Length - 1; i++)
{
  Console.Write("{0} <-> {1}:", strTimes[i], strTimes[i+1]);
  Console.Write("[string: {0}]", string.Compare(strTimes[i], strTimes[i + 1]));
  Console.Write("[date: {0}]", DateTime.Compare(DateTime.Parse(strTimes[i]), DateTime.Parse(strTimes[i + 1])));
  Console.WriteLine();
}
When I run this, I get the following output:
Code:
8:30 <-> 08:40:[string: 1][date: -1]
08:40 <-> 9:00:[string: -1][date: -1]
9:00 <-> 10:00:[string: 1][date: -1]
Explanation of actual comparison results:
8:30 <-> 08:40
  - As strings: first is greater (incorrect)
  - As datetimes: second is greater (correct)
08:40 <-> 9:00
  - As strings: second is greater (correct)
  - As datetimes: second is greater (correct)
9:00 <-> 10:00
  - As strings: second is greater (correct)
  - As datetimes: first is greater (incorrect)

Why the incorrect ones?
8:30 <-> 08:40
  - As strings, '0' comes before '8'.
  - As datetimes, clearly 8:30 is before 8:40
9:00 <-> 10:00
  - As strings: '1' comes before '9'
  - As datetimes: 9:00 comes before 10:00

What this all means is that doing a comparison between strings and dates will not yield the same result. This means that your query and/or code that uses string comparisons will have unexpected results.

It bothers me to see someone spending any amount of time trying to debug a problem that shouldn't even exist. The simple fact is that you need to change the data type you are using for these values otherwise you are going to have problems.

I am confident that if you change your data types, all your comparison problems will disappear.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
boolean value shows true and false in the datagrid debjanib ASP.NET 2.0 Basics 2 March 23rd, 2007 08:38 AM
XSLT - test the value is true or false haixia XSLT 1 August 21st, 2006 01:38 PM
Boolean True/False to Yes/No peterasimpson VB How-To 2 February 6th, 2006 07:58 PM
Yes/No in DB showing True/False in ASP query miller2000 Classic ASP Databases 2 June 17th, 2004 10:36 AM
Format rounding figure : 1235.00 => 1200.00 Jane SQL Language 1 June 27th, 2003 02:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.