Solved

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

Posted on 2013-12-11
5
363 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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