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
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