首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为什么DataSet 的值是空?该如何处理

2012-01-23 
为什么DataSet 的值是空??publicpartialclassReport1:System.Web.UI.Page{DataSetdsnewDataSet()protect

为什么DataSet 的值是空??
public   partial   class   Report1   :   System.Web.UI.Page
{
        DataSet   ds   =   new   DataSet();

  protected   void   btn_find_Click(object   sender,   EventArgs   e)
        {
                string   strconn   =   ConfigurationManager.AppSettings[ "dsn "];
                //连接本地计算机的WMS数据库
                SqlConnection   cn   =   new   SqlConnection(strconn);
                string   mysql   =   "SELECT   a.vin,   d.name   AS   operate,   a.operate_time,   a.warehouse,   b.name   AS   driver,   c.name   AS   operator   FROM   Transations   a   INNER   JOIN     Drivers   b   ON   a.driver   =   b.code   INNER   JOIN   Users   c   ON   a.operator   =   c.Uname   INNER   JOIN   TransationCode   d   ON   a.operate   =   d.code   where   (operate= 'movein ')   ";
                //如果仓库有输入
                if   (ddl_warehouse.SelectedIndex   !=   0)
                {
                        mysql   =   mysql   +   "and   a.warehouse= ' "   +   ddl_warehouse.SelectedItem.Text.ToString()   +   " ' ";
                }
                if   (tbx_time1.Text.ToString()   !=   " ")
                {
                        mysql   =   mysql   +   "and   a.operate_time> = ' "   +   tbx_time1.Text.ToString()   +   " ' ";
                }
                if   (tbx_time2.Text.ToString()   !=   " ")
                {
                        mysql   =   mysql   +   "and   a.operate_time <= ' "   +   tbx_time2.Text.ToString()   +   " ' ";
                }
                mysql   =   mysql   +   "   ORDER   BY   a.operate_time ";

                SqlDataAdapter   da   =   new   SqlDataAdapter(mysql,   cn);

                da.Fill(ds);

                GridView1.DataSource   =   ds;

                GridView1.DataBind();

                cn.Close();
              //查询得到一个ds
        }


  public   void   CreateExcel(DataSet   myds)//导出数据到execl的函数
        {
              ..........
        }
  protected   void   btn_export_Click(object   sender,   EventArgs   e)//导出数据
        {
              CreateExcel(this.ds)
        }
}

在btn_export_Click事件中调用this.ds,但是this.ds的值是空?请问是甚么原因呢

[解决办法]
btn_find_Click 点击以后DataSet的值并没有被 保存

楼主要清除asp.net是无状态的 每次访问的状态的不被保存的(除非楼主使用了缓存cache)

再你的btn_export_Click中
需要重新获取ds 的值
[解决办法]
看你的实际需要,你可以保存ds结果,也可以保存条件去查询。
可以用viewstate, 也可以用session. 下面的用viewstate. 自己权衡性能上的要求。

protected void btn_find_Click(object sender, EventArgs e)
{
string strconn = ConfigurationManager.AppSettings[ "dsn "];
//连接本地计算机的WMS数据库
SqlConnection cn = new SqlConnection(strconn);
string mysql = "SELECT a.vin, d.name AS operate, a.operate_time, a.warehouse, b.name AS driver, c.name AS operator FROM Transations a INNER JOIN Drivers b ON a.driver = b.code INNER JOIN Users c ON a.operator = c.Uname INNER JOIN TransationCode d ON a.operate = d.code where (operate= 'movein ') ";
//如果仓库有输入
if (ddl_warehouse.SelectedIndex != 0)
{
mysql = mysql + "and a.warehouse= ' " + ddl_warehouse.SelectedItem.Text.ToString() + " ' ";
}
if (tbx_time1.Text.ToString() != " ")
{
mysql = mysql + "and a.operate_time> = ' " + tbx_time1.Text.ToString() + " ' ";
}
if (tbx_time2.Text.ToString() != " ")
{
mysql = mysql + "and a.operate_time <= ' " + tbx_time2.Text.ToString() + " ' ";
}
mysql = mysql + " ORDER BY a.operate_time ";

SqlDataAdapter da = new SqlDataAdapter(mysql, cn);

da.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

cn.Close();
//查询得到一个ds
ViewState[ "ds "] = ds
}

protected void btn_export_Click(object sender, EventArgs e)//导出数据
{ if (ViewState[ "ds "] != null)
CreateExcel((DataSet)ViewState[ "ds "])
}

热点排行