日期:2014-05-18  浏览次数:20399 次

用强类型的时候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
  &n