Insert with FormView using Linq

February 5, 2009 by: B.Harding

I had a wierd error trying to insert data using a formview today.

The jist is this: the formview was trying to use an empty string for an int field instead of Nothing.
Here is the error I saw, helpful huh?

Failed to set one or more properties on type xxxxxxx  Ensure that the input values are valid and can be converted to the corresponding property types.

The fields are allowed to be null but the formview just isn’t smart enough to use nothing instead of “”.

My workaround was simple and flexible enough to be re-used so I figured I would post it here.
I simply hijack the formview’s onInserting and replaced “” with Nothing for each field.

Protected Sub formView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)

For Each o As DictionaryEntry In e.Values
If o.Value = “” Then e.Values(o.Key) = Nothing
Next

End Sub

Filed under: Web Development
Tags: , , ,

Comments

5 Responses to “Insert with FormView using Linq”
  1. JaneRadriges says:

    The article is ver good. Write please more

  2. GarykPatton says:

    Hello, can you please post some more information on this topic? I would like to read more.

  3. Hi. I like the way you write. Will you post some more articles?

  4. Nice post..Keep them coming :) Thanks for sharing.

  5. Excellent post..Keep them coming :) Thanks for sharing.

Leave a Reply