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

生活所迫在来问个问题。。哎。都不好意思了
Server   Error   in   '/ '   Application.
--------------------------------------------

Object   cannot   be   cast   from   DBNull   to   other   types.  
Description:   An   unhandled   exception   occurred   during   the   execution   of   the   current   web   request.   Please   review   the   stack   trace   for   more   information   about   the   error   and   where   it   originated   in   the   code.  

Exception   Details:   System.InvalidCastException:   Object   cannot   be   cast   from   DBNull   to   other   types.

Source   Error:  


Line   37:                   txtSp_content.Text   =   dt.Rows[0][3].ToString();
Line   38:                   txtSp_cost.Text   =   dt.Rows[0][4].ToString().Trim();
Line   39:                   txtSp_time.Text=   Convert.ToDateTime(dt.Rows[0][5]).ToString( "yyyy-MM-dd ");
Line   40:                   ddlSp_xianshi.SelectedValue   =   dt.Rows[0][6].ToString();
Line   41:           }
 

Source   File:   f:\usr\cn30120\admin\daili\edit.aspx.cs         Line:   39  

Stack   Trace:  


[InvalidCastException:   Object   cannot   be   cast   from   DBNull   to   other   types.]
      System.DBNull.System.IConvertible.ToDateTime(IFormatProvider   provider)   +54
      System.Convert.ToDateTime(Object   value)   +29
      admin_daili_edit.Bin(String   strsql)   in   f:\usr\cn30120\admin\daili\edit.aspx.cs:39
      admin_daili_edit.Page_Load(Object   sender,   EventArgs   e)   in   f:\usr\cn30120\admin\daili\edit.aspx.cs:24
      System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr   fp,   Object   o,   Object   t,   EventArgs   e)   +15
      System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object   sender,   EventArgs   e)   +34
      System.Web.UI.Control.OnLoad(EventArgs   e)   +99
      System.Web.UI.Control.LoadRecursive()   +47
      System.Web.UI.Page.ProcessRequestMain(Boolean   includeStagesBeforeAsyncPoint


他老报   转换这儿出问题   Line   39:                   txtSp_time.Text=   Convert.ToDateTime(dt.Rows[0][5]).ToString( "yyyy-MM-dd ");

请高手指点。。

------解决方案--------------------
dt.Rows[0][5] 看一下这个值 没空值

------解决方案--------------------
if(dt.Rows[0][5]==null || dt.Rows[0][5]== " ")
{
txtSp_time.Text= " ";
}
else
{
txtSp_time.Text= Convert.ToDateTime(dt.Rows[0][5]).ToString( "yyyy-MM-dd ");
}
------解决方案--------------------