How to handle blank or null from XML element while SQL Bulk copying

BeUnique 2,332 Reputation points
2022-02-01T05:05:49.447+00:00

If date field is blank from XML element then we are getting error.

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.

170092-image.png

below sample xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><RecSearch Val="175"><SearchRes>
<Employee EmpId="ME00001">
<EmpRetired>No</EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved>2018-08-20T23:00:00.000Z</DateRelieved>
</Employee>
<Employee EmpId="ME0002">
<EmpRetired></EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved>2018-08-20T10:00:00.000Z</DateRelieved>
</Employee>
<Employee EmpId="ME0003">
<EmpRetired></EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved></DateRelieved> =======> Date is blank.
</Employee>
</SearchRes>
</RecSearch>

170058-image.png

pls. give us the solution where to correct this error inside handling the loop if date is null...

Developer technologies | C#
Developer technologies | C#
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.
{count} votes

Answer accepted by question author
  1. Viorel 125.8K Reputation points
    2022-02-01T09:32:14.26+00:00

    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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.