How to check for null column in a DataRow

Coreysan 1,811 Reputation points
2023-09-26T00:26:22.24+00:00

I'm using DataTables in 4.7.2, and I have defined a table with 2 columns, Start_Date and End_Date.

If the 2nd column has a null value, how can I check for that? Right now I've gotten this far, which

abends at runtime:

// foreach (DataRow eachrow in myrows)

// {

// if (eachrow.Field<DateTime>("End_Date") == null) process_date = DateTime.Now;

// }

Developer technologies .NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-09-26T01:20:33.6533333+00:00

    Hi @Coreysan , Welcome to Microsoft Q&A,

    Logically there is nothing wrong with your code, but to safely check if eachrow.Field<DateTime>("End_Date") is null, you should use the IsNull method.

    I wrote a small example to demonstrate:

    using System;
    using System.Data;
    
    namespace _9_26_x
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                //Create a simple DataTable
                DataTable table = new DataTable();
                table.Columns.Add("Start_Date", typeof(DateTime));
                table.Columns.Add("End_Date", typeof(DateTime));
    
                //Add some sample data
                table.Rows.Add(DateTime.Parse("2023-09-01"), DateTime.Parse("2023-09-10"));
                table.Rows.Add(DateTime.Parse("2023-09-11"), DBNull.Value); // "End_Date" is null
                table.Rows.Add(DateTime.Parse("2023-09-21"), DateTime.Parse("2023-09-30"));
    
                DateTime process_date = DateTime.Now; //Default process_date
    
                // Loop through each row and check "End_Date"
                foreach (DataRow row in table.Rows)
                {
                    if (row.IsNull("End_Date"))
                    {
                        process_date = DateTime.Now; // Set process_date when "End_Date" is null
                    }
                    else
                    {
                        DateTime endDate = row.Field<DateTime>("End_Date");
                        // "End_Date" is not null, you can use endDate
                        Console.WriteLine("Start Date: " + row.Field<DateTime>("Start_Date"));
                        Console.WriteLine("End Date: " + endDate);
                    }
                }
    
                Console.WriteLine("Process Date: " + process_date);
                Console.ReadLine();
            }
        }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

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