• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

C# - assigning a dataset datetime object to a datetime field

Hi.

I have the following code:

conn = "MySqlConnection";
            conn.Open();
            SqlCommand cmd = new SqlCommand("spDaily", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            rdr = cmd.ExecuteReader();

            while (rdr.Read())
                DateTime SentDate = rdr["SentDate"];
rdr.Close();

The line in bold is giving me an error.  What should I do to assign the rdr["SentDate'] to a datetime field so that I may do a check to find out if the sent date is not today?
0
Jasmin01
Asked:
Jasmin01
  • 3
  • 2
1 Solution
 
sameer_goyalCommented:
Try this

DateTime SentDate = Convert.ToDateTime(rdr["SentDate"]);

Basically, rdr["SentDate"] returns an object and hence has to be explicitly converted to DateTime before it can be assigned to a DateTime object


even better, since DateTime will not accept null values, use this as a fail safe

if(rdr["SentDate"] != DBNull.Value)
{
     DateTime SentDate = Convert.ToDateTime(rdr["SentDate"]);
}


let me know if it doesn't work
0
 
Jasmin01Author Commented:
I am getting an error on :
DateTime SentDate = Convert.ToDateTime(rdr["SentDate"]);

Error: Embedded statement cannot be a declaration or labeled statement.
0
 
sameer_goyalCommented:
can you put a screen shot of your code for me?

or just put the code with atleast 10 lines above and below the line which is throwing exception
0
 
Jasmin01Author Commented:
Sorry, it did work, I just forgot to use brackets after the "While" statement.  Thanks.
0
 
sameer_goyalCommented:
happy to help :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now