Solved

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

Posted on 2013-12-11
5
360 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
ID: 39712732
Try instead just this :)
DateTime? dt = dtrow["MtlDueDate"] as DateTime?;

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39712739
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
ID: 39712745
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
ID: 39712748
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
ID: 39713434
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

17 Experts available now in Live!

Get 1:1 Help Now