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");请高手指点。。

解决方案 »

  1.   

    dt.Rows[0][5] 看一下这个值 没空值
      

  2.   

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

  3.   

    先判断一下dt.Rows[0][5]是不是为空。
    如果格式不正确也会报错的
      

  4.   

    异常页面已经说明了问题
    Object cannot be cast from DBNull
    =============================================System.Object date = dt.Rows[0][5];
    if(System.DBNull.Value != date)
        txtSp_time.Text = Convert.ToDateTime(date).ToString("yyyy-MM-dd");