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

Posted on 2012-09-17
Last Modified: 2012-09-17

I have the following code:

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

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

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?
Question by:Jasmin01
    LVL 5

    Accepted Solution

    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

    Author Comment

    I am getting an error on :
    DateTime SentDate = Convert.ToDateTime(rdr["SentDate"]);

    Error: Embedded statement cannot be a declaration or labeled statement.
    LVL 5

    Expert Comment

    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

    Author Closing Comment

    Sorry, it did work, I just forgot to use brackets after the "While" statement.  Thanks.
    LVL 5

    Expert Comment

    happy to help :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now