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

折磨人啊,寻解决之道~~~高手。。。
在html代码的 <Title> </Title> 里加入
<%=Convert.IsDBNull(dr1[ "name "])   ?   "没有数据 "   :   "22 "%> 要显示数据库的一个信息,后台代码
  public   DataRow   dr1;

    public   void   datalist1()
        {
                try
                {
                        SqlConnection   conn   =   new   SqlConnection( "server=.;uid=11;pwd=11;database=table1; ");
                        SqlDataAdapter   ad   =   new   SqlDataAdapter( "select   *   from   table1 ",   conn);

                        conn.Open();
                        DataSet   ds   =   new   DataSet();
                        ad.Fill(ds,   "table1 ");
                       
                        if(ds.Tables[ "table1 "].Rows.Count   <=0)
                        {
                                Response.Write   ( "记录集为空 ");
                        }
                        else
                        {
                        dr1   =   ds.Tables[ "table1 "].Rows[0];
                        DataList1.DataSource   =   ds;
                        DataList1.DataBind();
                        conn.Close();
                        }

                }
                catch   (Exception   me)
                {
                        Response.Write(me.Message);
                }
}

    出现的问题是~   如果数据库为空,不会报出try   catch的异常,也不会输入   “记录集为空”,直接出现     未将对象引用设置到对象的实例。如果字段为空则没有错误,但是数据库为空就这样,求解决办法~~~~~~~~~~~

------解决方案--------------------
SqlDataAdapter ad = new SqlDataAdapter( "select top 1 * from table1 ", conn);

//conn.Open();

//dr1 = ds.Tables[ "table1 "].Rows[0];
DataList1.DataSource = ds.Tables[ "table1 "];
DataL