views:

415

answers:

2

What is the right way to avoid errors on INSERT and UPDATES with a SqlDataAdapter/SqlCommandbuilder when the SQL Statement used to SELECT has a computed column as one of the return fields?

I get the error now that "The column Amount cannot be modified because it is either a computed column or is the result of a UNION operator".

UPDATE: I fixed the issue by using a query like this:

SELECT *, PercentRating * 500 AS CoreValue FROM ValueListings

And ditching the computed column. Now it works. How is that SqlCommandBuilder realizes to NOT build the CoreValue field into the UPDATE and INSERT statements? Anybody know how this works internally?

A: 

If you can avoid using the * it WILL save you trouble later. With * if you change the schema your code may break. If named fields, you're good.

Chris Woodruff
+1  A: 

Use The Sentence:

SELECT * FROM ValueListings

Then After Fill the DataTable, add a Computed Column to it:

  Dim Dt as new DataTable
  Da.Fill(Dt)
  Dt.columns.add("CoreValue", GetType(Double), "PercentRating * 500")
x77