ConvertEmptyStringToNull =“假”,但转换仍然发生 [英] ConvertEmptyStringToNull=”false” and yet the conversion still happens

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

问题描述

DetailsView控件绑定到ObjectDataSource控件。内部DetailsView的EditItemTemplate中是两个文本框(T1和T2)。 T1被映射到更新String类型的参数,而T2被映射到更新DateTime类型的参数。

假设这两个文本框包含一个空字符串,然后当我尝试通过单击DetailsView的更新按钮更新数据源,ODS(或者是它也许DetailsView控件)自动转换T1的空字符串为空,而T2的空字符串不被转换为null。我试图prevent ODS从T1的更新参数的ConvertEmptyStringToNull属性设置为false T1的空字符串转换为空(我以前还设置< ASP:的TemplateField ConvertEmptyStringToNull =false的...&GT ; ,但没有效果。

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

b)还,哪能prevent转换(顺便说一句 - 我知道我可以转换成空回空字符串update方法里面)

感谢名单


解决方案

  

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


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


  

B)另外,我怎么能prevent的
  转换(顺便说一句 - 我知道我可以
  转换空回空字符串
  内部更新方法)?


编辑:我试图复制上面的问题,但我只能重复的问题时,我没有指定 ConvertEmptyStringToNull =FALSE< ASP:的TemplateField> 绑定控件的< ASP:参数> &LT的; ASP:ObjectDataSource控件> 。如果你离开了任,那么你会得到一个空场空值。随着 ConvertEmptyStringToNull =FALSE在这两个地方不空字符串转换为空值定义。空字符串传递正确。你说,你没有尝试在这两个地方,所以我不知道为什么它不为你工作。也许你能告诉我们你的数据源和DetailsView标记。

随着中说,我认为它仍然是一个好主意,使您的业务类下面描述的检查。就像你说的,你可以转换空回一个空字符串。这就是我做它:

我有一个辅助类,让我们把它叫做BizObject,包含此方法:

 受保护的静态字符串ConvertNullToEmptyString(字符串输入)
{
  返回(输入== NULL:输入?);
}

然后在我的商业类的插入/更新我称之为ConvertNullToEmptyString每个字符串参数:

 公共静态布尔UpdateSource(字符串SOURCENAME,日期sourceDate)
{
    SOURCENAME = BizObject.ConvertNullToEmptyString(SOURCENAME);
    ...
    布尔RET = UpdateSource(记录);
    返回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

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆