Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Declaring DateTime variables and comparing them

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anil Gupte - 29 Mar 2008 18:04 GMT
I am using the followig code:

SqlConnection con = new SqlConnection(strcon);
con.Open();
string strSelect = "Select PaidUntil from Users where username='" + un +
"'";
SqlCommand cmd = new SqlCommand(strSelect, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
DateTime PaidUntil;
PaidUntil = DateTime.Parse(dr.GetValue(0));
if (PaidUntil < DateTime.Now)
The problem is with the line where I am trying to declare the PaidUntil
local variable.  I get the error:
Compiler Error Message: CS1502: The best overloaded method match for
'System.DateTime.Parse(string)' has some invalid arguments

Also, I have not got to the comparison part, but is that correct or should I
be using something like:
DateTime.Compare(PaidUntil, DateTimeNow())

Thanx for your help.

Signature

Anil Gupte
www.keeninc.net
www.icinema.com

Just Me - 29 Mar 2008 18:37 GMT
You need to step through the code in debug. but you should try adding
.ToString() to your dr.GetValue(0), this may help. You need to know though
that your object is not null.

>I am using the followig code:
>
[quoted text clipped - 19 lines]
>
> Thanx for your help.
Peter Duniho - 29 Mar 2008 18:50 GMT
> [...]
> PaidUntil = DateTime.Parse(dr.GetValue(0));
[quoted text clipped - 3 lines]
>  Compiler Error Message: CS1502: The best overloaded method match for
> 'System.DateTime.Parse(string)' has some invalid arguments

You probably just need to cast the result from GetValue() to a string.  
GetValue() returns an object reference, not a string.

Of course, it goes without saying that if you expect this code to work,  
the actual object had better actually be a string.  If not, you have some  
additional conversion to do.

The comparison looks fine, but why bother asking about it until you have  
problems?

Pete
thomasnguyencom - 29 Mar 2008 18:51 GMT
> I am using the followig code:
>
[quoted text clipped - 22 lines]
> --
> Anil Guptewww.keeninc.netwww.icinema.com

I've used:

DateTime PaidUntil = System.Convert.ToDateTime(someObject);

Also, keep in mind for comparing, the entire DateTime object contains
the both the Date and Time. Otherwise, be sure to get just the
PaidUntil.Date. I've busted myself a few times on that one.

Good luck,
-tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.