Error in datetime conversion if XML element date value is null

Gani_tpt 1,466 Reputation points

If date field is blank in XML file i am getting error.

see the below screenshot

I am using foreach loop, because i have multiple records in my xml file.

The code is working fine except the date error (if date is blank in xml element ==> Third Record "DateRelieved" is blank.


below sample xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><RecSearch Val="175"><SearchRes>
<Employee EmpId="ME00001">
<Employee EmpId="ME0002">
<Employee EmpId="ME0003">
<DateRelieved></DateRelieved> =======> Date is blank.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,165 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.5K Reputation points

    Try executing this fragment:

    var c = dataSet.Tables["Employee"].Columns["DateRelieved"];
    foreach( DataRow dr in dataSet.Tables["Employee"].Rows )
       if( string.IsNullOrWhiteSpace( dr.Field<string>( c ) ) ) dr[c] = null;

    Put it before copy.WriteToServer.

0 additional answers

Sort by: Most helpful