Declaring DateTime variables and comparing them

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

Just Me

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

thomasnguyencom

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top