string sql="select count(*) from qcqg where uid='2'";
cmd = new SqlCommand(sql,conn);
int i=(int)cmd.ExecuteScalar();
if(i>0)
countreg=i;
else
Response.Write (目前还没有任何信息!");

解决方案 »

  1.   

    上面的语句中应该在开始部分打开数据库,在if(i>0)之前关闭数据库连接。
    要绑定数据在DataGrid或DataList,如果绑定的不是count(i)的值,当然还应该写另一条sql语句,类似这样:
    sql="select * from qcqg where uid='2'";
    SqlDataAdapter adapter=new SqlDataAdapter(sql,conn);
    DataTable table=new DataTable();
    adapter.Fill(table);
    DataGrid1.DataSource=table;
    DataGrid1.DataBind();
      

  2.   

    放一个label或者literal在你的aspx文件里,然后根据结果设:
    theLabel.Text = read.GetValue(0).ToString();
    else
    theLabel.Text = "目前还没有任何信息!";
      

  3.   

    按照dawave()的指点改写了代码,但运行时还是一样,不会提示"目前还没有任何消息!"这样的提示语句,而只会显示0这一记录总数,这和我原来的显示格式一样,虽然统计结果没错,但我希望是显示提示语句,哪位再给指点一下好吗?谢谢了。