Why are these NOT equal?

  • Thread starter Thread starter Bob Bartel
  • Start date Start date
B

Bob Bartel

I have a routine to compare the current value with the original value of
fields in a Datatable. The code loops through each column to compare the
values and builds an update statement for those columns that have changed.

But for some reason, the Datetime fields are evaluating as not equal when
they are! (I just retreived the row and did nothing with the Datetime
columns, and in the debugger "a" and "b" look the same).

Here is the code:

DataRow Row = MyTable.Rows[0];

foreach (DataColumn column in Columns)
{
a = Row[column.ColumnName, DataRowVersion.Current];
b = Row[column.ColumnName, DataRowVersion.Original];

if (b != a)
{
//Build Update Statement
}
}

Any thoughts anyone?

Thanks in advance.

Bob Bartel
 
Bob Bartel said:
I have a routine to compare the current value with the original value of
fields in a Datatable. The code loops through each column to compare the
values and builds an update statement for those columns that have changed.

But for some reason, the Datetime fields are evaluating as not equal when
they are! (I just retreived the row and did nothing with the Datetime
columns, and in the debugger "a" and "b" look the same).

Here is the code:

DataRow Row = MyTable.Rows[0];

foreach (DataColumn column in Columns)
{
a = Row[column.ColumnName, DataRowVersion.Current];
b = Row[column.ColumnName, DataRowVersion.Original];

if (b != a)
{
//Build Update Statement
}
}

Any thoughts anyone?

How are a and b declared? They're probably as "object" in which case
the value is the boxed value and you're just checking for referential
equality.

Try object.Equals(a, b)
 
Thank you. You nailed it!

bob


Jon Skeet said:
Bob Bartel said:
I have a routine to compare the current value with the original value of
fields in a Datatable. The code loops through each column to compare the
values and builds an update statement for those columns that have
changed.

But for some reason, the Datetime fields are evaluating as not equal when
they are! (I just retreived the row and did nothing with the Datetime
columns, and in the debugger "a" and "b" look the same).

Here is the code:

DataRow Row = MyTable.Rows[0];

foreach (DataColumn column in Columns)
{
a = Row[column.ColumnName, DataRowVersion.Current];
b = Row[column.ColumnName, DataRowVersion.Original];

if (b != a)
{
//Build Update Statement
}
}

Any thoughts anyone?

How are a and b declared? They're probably as "object" in which case
the value is the boxed value and you're just checking for referential
equality.

Try object.Equals(a, b)
 
Back
Top