Declaring DateTime variables and comparing them

  • Thread starter Thread starter Anil Gupte
  • Start date Start date
A

Anil Gupte

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.
 
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:

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.

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
 
Back
Top