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();
}
}
}
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.