FormView, empty TextBox gives error: Input string was not in a cor

N

Neil Young

Hello group,

I'm referring to a "rather old thread" (April 21st 2005). Because I also ran
into problems with ASP.NET 2.0 formview and DBNull, but found a way around,
I would like to share my experiences here.

The problem: A given datatable containing nullable datetime or integer
column(s) cannot be fed by a formview, if the edited or inserted date string
is empty. The same in similarity with other typed columns (not tested). This
is primarily caused by a lack of functionality of a formview compared
against gridview or detailsview (Hope this will be fixed somehow in the
final ASP.NET 2.0). The latter two controls do have a property called
"ConvertNullToDbNull" for bound fields, what does exactly what is required:
Convert an empty string to a DBNull.

I did find a way around: One has to provide event handlers for both the
FormView_ItemUpdating and FormView1_ItemInserting events. The handlers could
look like these, if one has a "date" and/or an "integer" column which are
nullable and shall be setup with DBNull:

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs
e) {
if (e.NewValues["date"].ToString() == String.Empty)
e.NewValues["date"] = null;
if (e.NewValues["integer"].ToString() == String.Empty)
e.NewValues["integer"] = null;
}

protected void FormView1_ItemInserting(object sender,
FormViewInsertEventArgs e) {
if (e.Values["date"].ToString() == String.Empty)
e.Values["date"] = null;
if (e.Values["integer"].ToString() == String.Empty)
e.Values["integer"] = null;
}

This trick did the job for me: I'm now able to provide DBNull values with a
formview as it is possible w/o probs with gridview and detailsview.

HTH
Neil
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top