This is the second time I've seen this error message today. I got some help earlier on, but now it seems to be happening with a date comparison function that I have written.
Whenever the user presses the Overdue button, all of the rows in the datagridview need to by updated by column 1 (DatePaid).
Unfortunately this doesn't seem to be working and I get the error message:
An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe
Additional information: Object reference not set to an instance of an object.
at:
DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());
My code is:
public void viewOverdue_Click(object sender, EventArgs e)
{
viewOverdue.ForeColor = Color.Red;
viewHire.ForeColor = Color.Black;
viewRent.ForeColor = Color.Black;
DateTime overdueDate = default(DateTime);
DateTime today = DateTime.Now;
string odDate = null;
if (today.DayOfWeek == DayOfWeek.Monday)
{
overdueDate = today.AddDays(-12);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Tuesday)
{
overdueDate = today.AddDays(-13);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Wednesday)
{
overdueDate = today.AddDays(-7);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Thursday)
{
overdueDate = today.AddDays(-8);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Friday)
{
overdueDate = today.AddDays(-9);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Saturday)
{
overdueDate = today.AddDays(-10);
odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
}
else if (today.DayOfWeek == DayOfWeek.Sunday)
{
overdueDate = today.AddDays(-11);
odDate = overdueDate.Date.ToString("dd/MM/yyy HH:mm:ss");
}
CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
manager.SuspendBinding();
DateTime dateBase = DateTime.Parse(odDate);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());
row.Visible = (dateRow <= dateBase);
}
manager.ResumeBinding();
}