Solved

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

Posted on 2013-12-11
5
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 30

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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