本文介绍了ConvertEmptyStringToNull =“false”,但转换仍然发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DetailsView绑定到ObjectDataSource。里面的Detailsview的EditItemTemplate是两个文本框(T1和T2)。 T1被映射到类型为String的更新参数,而T2被映射到类型为DateTime的更新参数。假设两个文本框都包含一个空字符串,那么当我尝试通过点击DetailsView的Update按钮来更新数据源时,ODS(或者它也许是DetailsView)会自动转换T1的空字符串为空,而T2的空字符串不会转换为空。我试图通过将T1的更新参数的ConvertEmptyStringToNull属性设置为false(我也设置了< asp:TemplateField ConvertEmptyStringToNull =false...> ,但没有任何效果。



a)任何想法,为什么T1的空字符串被转换,而T2不是?



b)另外,我如何防止转换(BTW - 我意识到我可以将null恢复为空字符串更新方法)?



thanx

解决方案

T2是一个值类型的DateTime。值类型不能为空。好吧,除非你使用

编辑:我试图复制问题但是当我没有在< asp:TemplateField> ConvertEmptyStringToNull =false时,我只能复制问题< asp:ObjectDataSource>中的< asp:Parameter> 的绑定控件 AND 。如果你离开,那么你将得到一个空字段的空值。使用在两个地方定义的 ConvertEmptyStringToNull =false,它不会将空字符串转换为空值。空字符串被正确传递。你说你在两个地方都尝试过,所以我不知道为什么它不适合你。也许您可以向我们显示您的数据源和详细信息标记。



正因为如此,我认为在您的业务类中进行以下检查仍然是一个好主意。就像你说的,你可以将null转回一个空字符串。这是我如何做到的:



我有一个帮助类,让它调用它包含这个方法的BizObject:

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

然后在我的业务类的插入/更新方法中,我对每个字符串参数调用ConvertNullToEmptyString :

  public static bool UpdateSource(string sourceName,DateTime sourceDate)
{
sourceName = BizObject.ConvertNullToEmptyString (sourceName);
...
bool ret = UpdateSource(record);
return ret;
}


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

解决方案

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

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

这篇关于ConvertEmptyStringToNull =“false”,但转换仍然发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:03