newbie: DataSet.HasChanges()

D

Dan

Hi all, I'm trying to create a 'smart' equivalent of the
DataSet.HasChanges() method which can tell me if there are no 'true' changes
in a dataset; e.g. when user enters "Alice" in a name field and the old
value was "Alice" too, the DataSet.HasChanges() method rets true but the two
values are equal so that I should not prompt the user to save its changes
when editing such record.

To do this, I was thinking of a function which receives a dataset and cycles
through all the tables and (changed) rows fields comparing the original
version with the current version to see if they are actually different.
Anyway, how can I implement in a general way comparison between two fields
without knowing in advance their type? I could compare the reference for
row[i, DataRowVersion.Current] with that of row[i, DataRowVersion.Original]
to see if they are equal (which means that their value is the same), but
this does not work in all cases (e.g. it works for strings but not for
integers). Could anyone give a hint? A sample codepiece follows...

Thanks!
===================================
public bool SmartHasChanges(DataSet ds)
{
if (!ds.HasChanges()) return false;

foreach (DataTable dt in ds.Tables)
{
DataTable dtChanges = dt.GetChanges();
if (dtChanges != null)
{
foreach (DataRow row in dtChanges.Rows)
{
switch (row.RowState)
{
case DataRowState.Added:
return true;
case DataRowState.Deleted:
return true;
case DataRowState.Modified:
DumpVersions(row);
for (int i = 0; i < dt.Columns.Count; i++)
{
if ( (row.HasVersion(DataRowVersion.Current)) &&
(row[i, DataRowVersion.Current] !=
row[i, DataRowVersion.Original]) )
return true;
}
break;
}
}
}
}
return false;
}
 
C

Cor Ligthert

Dan,

Did you calculate the change that what you are checking now will exist.
Probably the code you are using now will cost a lot more time than what you
want to win.

Looks for me something as people who wants forever pessimistic concurrency
instead of optimistic concurrency.

See I wrote "forever" because there are in my opinion situations there
should be pessimistic concurrency be used.

Just my thought,

Cor
 
U

Uri Dor

I agree with Cor.
If, however, you think this will save a lot, it's also possible to avoid
this by not attempting to make such "non-changes" to the dataset, or by
calling AcceptChanges() when such a "non-change" occured, using
ColumnChanging/ed or RowChanging/ed.
 
J

Jay B. Harlow [MVP - Outlook]

Dan,
Anyway, how can I implement in a general way comparison between two fields
without knowing in advance their type?
(row[i, DataRowVersion.Current] !=
row[i, DataRowVersion.Original]) )
Use Object.Equals as most if not all types normally in a DataTable support
equality, so they overload it.

Something like (not syntax checked):
if ( (row.HasVersion(DataRowVersion.Current)) &&
! row[i, DataRowVersion.Current].Equals(row[i,
DataRowVersion.Original]) )

Alternatively you could use Comparer.Default or Comparer.DefaultInvariant to
get an instance of a culture specific (culture invariant specific) comparer
then use Comparer.Compare to compare the two objects. Comparer.Compare
assumes that at least one of the objects implement IComparable (which most
if not all types in a DataTable do). Comparer can be found in the
System.Collections namespace...

NOTE: You may want to consider the DataSet.CaseSensitive & DataSet.Locale
(DataTable.CaseSensitive & DataTable.Locale) properties when creating the
Comparer to ensure that the StringComparer used is correct for the
"datatable" itself... I always thought it odd there was not a
DataSet.Comparer (DataTable.Comparer) property or DataSet.Compare method...

Hope this helps
Jay


Dan said:
Hi all, I'm trying to create a 'smart' equivalent of the
DataSet.HasChanges() method which can tell me if there are no 'true'
changes in a dataset; e.g. when user enters "Alice" in a name field and
the old value was "Alice" too, the DataSet.HasChanges() method rets true
but the two values are equal so that I should not prompt the user to save
its changes when editing such record.

To do this, I was thinking of a function which receives a dataset and
cycles through all the tables and (changed) rows fields comparing the
original version with the current version to see if they are actually
different. Anyway, how can I implement in a general way comparison between
two fields without knowing in advance their type? I could compare the
reference for row[i, DataRowVersion.Current] with that of row[i,
DataRowVersion.Original] to see if they are equal (which means that their
value is the same), but this does not work in all cases (e.g. it works for
strings but not for integers). Could anyone give a hint? A sample
codepiece follows...

Thanks!
===================================
public bool SmartHasChanges(DataSet ds)
{
if (!ds.HasChanges()) return false;

foreach (DataTable dt in ds.Tables)
{
DataTable dtChanges = dt.GetChanges();
if (dtChanges != null)
{
foreach (DataRow row in dtChanges.Rows)
{
switch (row.RowState)
{
case DataRowState.Added:
return true;
case DataRowState.Deleted:
return true;
case DataRowState.Modified:
DumpVersions(row);
for (int i = 0; i < dt.Columns.Count; i++)
{
if ( (row.HasVersion(DataRowVersion.Current)) &&
(row[i, DataRowVersion.Current] !=
row[i, DataRowVersion.Original]) )
return true;
}
break;
}
}
}
}
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top