Q: Expressions in DataColumns

  • Thread starter Thread starter G .Net
  • Start date Start date
G

G .Net

Hi

I've run into a problem when using Expressions in a DataColumn of a
DataTable which I'm updating back to the underlying databas. That is, I'm
loading data into a DataTable in which I've set some of the DataColumns to
calculate their contents using the Expression property.

This all works fine until I try and transfer these calculated values back to
the SQL database. When I try to do this, I get the exception message:

"The column mapping from SourceColumn 'Col 1' failed because the DataColumn
'Col 1' is a computed column"

Is there anyway I can override this exception so the values are written back
to the database?

Geoff
 
You can handle the RowUpdating event and the adapter and copy the value from
the DataRow to the appropriate parameter on the command.

For example,
SqlDataAdapter adapter = ...;
adapter.RowUpdating += delegate(object sender, SqlRowUpdatingEventArgs args)
{
args.Command.Parameters["X"].Value = args.Row["X"];
};
adapter.Update(...);
 
Hi Mark

I'm afraid I don't follow. Could you explain further?

G

Mark Ashton said:
You can handle the RowUpdating event and the adapter and copy the value
from
the DataRow to the appropriate parameter on the command.

For example,
SqlDataAdapter adapter = ...;
adapter.RowUpdating += delegate(object sender, SqlRowUpdatingEventArgs
args)
{
args.Command.Parameters["X"].Value = args.Row["X"];
};
adapter.Update(...);

G .Net said:
Hi

I've run into a problem when using Expressions in a DataColumn of a
DataTable which I'm updating back to the underlying databas. That is, I'm
loading data into a DataTable in which I've set some of the DataColumns
to
calculate their contents using the Expression property.

This all works fine until I try and transfer these calculated values back
to
the SQL database. When I try to do this, I get the exception message:

"The column mapping from SourceColumn 'Col 1' failed because the
DataColumn
'Col 1' is a computed column"

Is there anyway I can override this exception so the values are written
back
to the database?

Geoff
 

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

Back
Top