开发者

ConvertEmptyStringToNull=”false” and yet the conversion still happens

开发者 https://www.devze.com 2022-12-18 09:35 出处:网络
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 paramete

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 als开发者_开发百科o 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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消