views:

233

answers:

1

hi

DetailsView is bound to ObjectDataSource. Inside Detailsview’s EditItemTemplate are two TextBoxes ( T1 and T2 ). T1 is mapped to update parameter of type String, while T2 is mapped to update parameter of type DateTime.

Assuming both TextBoxes contain an empty string, then when I try to update the data source by clicking on DetailsView’s Update button, ODS ( or is it perhaps DetailsView ) automatically converts T1’s empty string to null, while T2’s empty string doesn’t get converted to null. I’ve tried to prevent ODS from converting T1’s empty string to null by setting T1’s update parameter’s ConvertEmptyStringToNull property to false ( I ‘ve also set <asp:TemplateField ConvertEmptyStringToNull=”false” …>, but to no effect.

a)Any idea why T1’s empty string gets converted, while T2’s doesn’t?

b) Also, how can I prevent the conversion( BTW - I realize I could convert null back to empty string inside update method )?

thanx

+1  A: 

a)Any idea why T1’s empty string gets converted, while T2’s doesn’t?

T2 is a DateTime which is a value type. Value types can't be null. Well unless you use the Nullable type

b) Also, how can I prevent the conversion( BTW - I realize I could convert null back to empty string inside update method )?

EDIT: I've tried to duplicate the problem above, but I could only duplicate the problem when I didn't specify ConvertEmptyStringToNull="false" in the <asp:TemplateField> of the bound control AND the <asp:Parameter> of the <asp:ObjectDataSource>. If you leave either out then you will get the null value on an empty field. With the ConvertEmptyStringToNull="false" defined in both places it does not convert the empty string to a null value. The empty string is passed correctly. You said that you did try it in both places, so I'm not sure why it's not working for you. Maybe you could show us your datasource and detailsview markup.

With that said I think it is still a good idea to make the check described below in your business class. Like you said you can convert null back to an empty string. This is how I have done it:

I have a helper class, lets call it BizObject, that contains this method:

protected static string ConvertNullToEmptyString(string input)
{
  return (input == null ? "" : input);
}

Then in my business class's Insert/Update method I call ConvertNullToEmptyString on each string parameter:

public static bool UpdateSource(string sourceName, DateTime sourceDate)
{
    sourceName = BizObject.ConvertNullToEmptyString(sourceName);
    ...
    bool ret = UpdateSource(record);
    return ret;
}
David Glass
A) “Good question, I'm not sure why either,…” Are you implying that you have the same issue where you set ConvertEmptyStringToNull to false and yet ODS still converts the value?B) So in essence ODS manages to convert empty string to DateTime.MinValue (assuming parameter is of type DateTime), but it isn’t able to convert null to empty string when passing this value to a method?!That's a bit odd
carewithl
@carewithl A) I did try what you said and it still did convert the empty string to null, but I usually do what I described above. B) No the ODS doesn't convert an empty string to DateTime.MinValue the MinValue is the default value for a DateTime.
David Glass
See my EDIT in my answer.
David Glass