Solved

String was not recognized as a valid DateTime error on Convert.ToDateTime

Posted on 2013-12-11
5
355 Views
Last Modified: 2014-08-28
I loading values from the database to a dataset.  When there is a null in the datetime column, the FormatException  is thrown - String was not recognized as a valid DateTime.  How do I handle this problem in the following method.

 public dsInventory PopulateClass(DataSet ds)
        {
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            dsInventory dsI = new dsInventory();
            foreach (DataRow dtrow in dt.Rows)
            {
                //Error on these lines when a null is available
                dsI.MtlDueDate = Convert.ToDateTime(dtrow["MtlDueDate"].ToString());
                dsI.MtlReceiptDate = Convert.ToDateTime(dtrow["MtlReceiptDate"].ToString());
                         

            }
            return dsI;
          }
0
Comment
Question by:cookiejar
5 Comments
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
Comment Utility
Try instead just this :)
DateTime? dt = dtrow["MtlDueDate"] as DateTime?;

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
Use DBNull.Value.Equals on the object without converting it to a string.

Here's an example:
   if (! DBNull.Value.Equals(row[fieldName]))
   {
      //not null
   }
   else
   {
      //null
   }
0
 
LVL 11

Expert Comment

by:Angelp1ay
Comment Utility
If you want your field to be a plain DateTime instead of the Nullable DateTime (DateTime?) I proposed then you need to do what ChloesDad said and probably want to set your field to DateTime.MinValue.

Personally I think DateTime? represents your data semantics more correctly.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
First, you may need to check values for DbNull:

if (row["myCol"] != DBNull.Value)
{
         ... //some code
}
else
{

}

Open in new window


In 'else' you may set your dateTime value to DateTime.MinValue or DateTime.MaxValue instead.

Other option would be to use Nullable of DateTime.
0
 
LVL 10

Expert Comment

by:Monica P
Comment Utility
Declaring as like below ., will accept the nothing in date field
Try like this in your code and check
 
 Dim DOB As Nullable(Of Date) = Date.Now
    DOB = Nothing

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

14 Experts available now in Live!

Get 1:1 Help Now