我有一个按钮,<asp:button id="after" text="下一条" runat=server> 作用是取数据库中的数据,一条一条显示在界面上。
k是我定义的全局变量,public static int k=0;                                              DataRow row=ds.Tables[0].Rows[k];
sick_id.Text=row["sick_id"].ToString();
sick_name.Text=Convert.ToString(row["sick_name"]);
dropdownlist2.Items[0].Text=Convert.ToString(row["come_status"]);
dropdownlist1.Items[0].Text=Convert.ToString(row["sex"]);
dropdownlist3.Items[0].Text=Convert.ToString(row["birth_date"]).Substring(0,4);
dropdownlist4.Items[0].Text=Convert.ToDateTime(row["birth_date"]).ToString("yyy-MM-dd").Substring(5                                                                                                               ,2);
Dropdownlist16.Items[0].Text=Convert.ToDateTime(row["birth_date"]).ToString("yyy-MM-dd").Substring(8,2);
k=k+1;在本地是没有错误的,一条一条可以显示出来,但是我上传到空间上面,就出现了以下的错误,错误如下:
我用的是.net 2003开发的,是不是和空间的.net framework  version有关系啊。
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: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  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
   _800service.index2.look_up(String sql, Int32 k) in e:\asp.net\800service\index2.aspx.cs:1101
   _800service.index2.after_Click(Object sender, EventArgs e) in e:\asp.net\800service\index2.aspx.cs:1375
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102 
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832 

解决方案 »

  1.   

    Object cannot be cast from DBNull to other types.  ====================================不能将 空 转换为其他对象你看看数据库那个 <null>是不是有关系
      

  2.   

    检查下你的数据源中有没有字段的数据是null
      

  3.   

    有的,但是有几个字段是null ,我测试只显示了四个字段,里面有null,是可以的,但是在往下有的就不行了啊。
      

  4.   

    有可能是你的日期没有处理好Convert.ToDateTime(row["birth_date"])row["birth_date"] 如果是空的话会出错吧。而且你是强制转换。加个判断或者在数据进入数据库之前,如果日期是空,自动赋值1900-01-01