当前位置: 代码迷 >> ASP.NET >> 对象不能从 DBNull 转换为其他类型(利用DetailsView绑定的),该如何解决
  详细解决方案

对象不能从 DBNull 转换为其他类型(利用DetailsView绑定的),该如何解决

热度:7657   发布时间:2013-02-25 00:00:00.0
对象不能从 DBNull 转换为其他类型(利用DetailsView绑定的)
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
  DataSourceID="AccessDataSource2" Height="50px" Width="532px" 
  BackColor="#E0E0E0" BorderWidth="0px" CellPadding="4" CellSpacing="1" 
  HorizontalAlign="Center">
  <FieldHeaderStyle HorizontalAlign="Left" />
  <Fields>
  <asp:BoundField DataField="Subtotal" HeaderText="小计" 
  SortExpression="Subtotal" DataFormatString="{0:C0}" HtmlEncode="False" >
  <ItemStyle HorizontalAlign="Center" Width="72px" />
  </asp:BoundField>
  <asp:TemplateField HeaderText="运费(固定运费10元)">
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:C0}",10) %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center" Width="72px" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="总计">
  <ItemTemplate>
  <asp:Label ID="Label3" runat="server" 
  Text='<%# String.Format("{0:C0}",Convert.ToInt32(Eval("Subtotal"))+10) %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center" Width="72px" />
  </asp:TemplateField>
  </Fields>
  <RowStyle BackColor="White" Height="24px" />
  </asp:DetailsView>
这是中间的一部分代码源错误如下:
源错误: 


行 110: <asp:TemplateField HeaderText="总计">
行 111: <ItemTemplate>
行 112: <asp:Label ID="Label3" runat="server" //System.InvalidCastException: 对象不能从 DBNull 转换为其他类型
行 113: Text='<%# String.Format("{0:C0}",Convert.ToInt32(Eval("Subtotal"))+10) %>'></asp:Label>
行 114: </ItemTemplate>
 
求救啊!

------解决方案--------------------------------------------------------
似乎不行吧。
------解决方案--------------------------------------------------------
Eval("Subtotal")
空值?
------解决方案--------------------------------------------------------
<%# String.Format("{0:C0}",(Eval("Subtotal") == null ? 0 : Convert.ToInt32(Eval("Subtotal"))+10)) %>