用强类型的时候DBNull的问题
我是学习Scott Mitchell的ASP.NET2.0教程,在学DataList和Repeater数据分页 的时候出现的问题.
原教程地址http://lovecherry.cnblogs.com/lovecherry/archive/2006/07/02/440840.html
具体是,先生成一个强类型的dataset,数据库就一张表,表名book.dataset的方法是getbook().
dataset作为数据访问层,另写了个bookBLL类作为数据业务层,然后通过ObjectDataSource绑定bookBLL到Repeater上去.
现在问题是,在bookBLL中如果是直接getbook(),能正常显示,如果在bookBLL中用PagedDataSource实现分页,就会出问题.
显示Unable to cast object of type 'System.DBNull ' to type 'System.String '
[InvalidCastException: Unable to cast object of type 'System.DBNull ' to type 'System.String '.]
bookRow.get_rebody() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\zyweb\50cca4dd\192790c4\App_Code.aug4dfc9.5.cs:750
[StrongTypingException: The value for column 'rebody ' in table 'book ' is DBNull.]
bookRow.get_rebody() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\zyweb\50cca4dd\192790c4\App_Code.aug4dfc9.5.cs:753
[TargetInvocationException: Property accessor 'rebody ' on object 'zybook+bookRow ' threw the following exception: 'The value for column 'rebody ' in table 'book ' is DBNull. ']
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +377
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +78
System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts) +60
System.Web.UI.DataBinder.Eval(Object container, String expression) +75
System.Web.UI.TemplateControl.Eval(String expression) +120
ASP.message_msg2_msg2_aspx.__DataBind__control5(Object sender, EventArgs e) in f:\zyweb\message\msg2\msg2.aspx:151
System.Web.UI.Control.OnDataBinding(EventArgs e) +80
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +198
System.Web.UI.Control.DataBind() +12
System.Web.UI.Control.DataBindChildren() +214
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +208
System.Web.UI.Control.DataBind() +12
System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +130
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +54
System.Web.UI.WebControls.Repeater.DataBind() +62
System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +13
System.Web.UI.Control.PreRenderRecursiveInternal() +88
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5731
我baidu一下,说是因为数据库中有空字段,而我的数据库中不可能避免空字段.
我找了出错的地方.原代码如下
public string rebody {
get {
try {
return ((string)(this[this.tablebook.rebodyColumn]));
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException( "The value for column \ 'rebody\ ' in table \ 'book\ ' is DBNull. ", e);
}
}
set {
this[this.tablebook.rebodyColumn] = value;
}
}
我改成了get {
try
{
if (this[this.tablebook.rebodyColumn] == DBNull.Value)
{
return " ";
}
else
{
return ((string)(this[this.tablebook.rebodyColumn]));
}
}
可是还是错,请问各位大神,该怎么改?另外再问下,有2个cs文件,我开始的时候改的是192790c4\Sources_App_Code下的一个cs,后面发现改了怎么保存都没有效果,后来直接改的是192790c4/下的一个cs文件.这2个cs文件一样,我想问有什么区别?
[解决办法]
因爲this[this.tablebook.rebodyColumn]為null
[解决办法]
DBNull.Value
这是空值呀
[解决办法]
try:
public string rebody {
get {
try {
return (this[this.tablebook.rebodyColumn] as string);
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException( "The value for column \ 'rebody\ ' in table \ 'book\ ' is DBNull. ", e);
}
}
set {
this[this.tablebook.rebodyColumn] = value;
}
}