protected void Page_Load(object sender, EventArgs e)
    {
        string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MY program\\WebSites\\wap_asp\\access\\image.mdb";
        OleDbConnection olecon = new OleDbConnection(strcon);
        OleDbCommand olecmd = new OleDbCommand("SELECT no,imagename FROM [image]", olecon);
                DataTable dt = new DataTable("image");        OleDbDataAdapter oda = new OleDbDataAdapter(olecmd);
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);        oda.Fill(ds,"image");        this.TextBox1.Text = ds.Tables["image"].Rows[0][1].ToString();        this.img1.ImageUrl = this.TextBox1.Text.ToString();
       
        
    }
    protected void Command1_Click(object sender, EventArgs e)
    {
        
     
    }
我在上面的大{}内定义的dataset到下面的单击按钮中就不能用了,我定义的东西都只能在同一个大{}能用,请达人多批评?

解决方案 »

  1.   

    出了Page_Load的作用域,这个ds对象就被回收了,所以需要定义到类里面,作为该窗体类的一个局部变量。才能在该类的所有方法中使用。
      

  2.   

    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
        {
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\MY program\\WebSites\\wap_asp\\access\\image.mdb";
            OleDbConnection olecon = new OleDbConnection(strcon);
            OleDbCommand olecmd = new OleDbCommand("SELECT no,imagename FROM [image]", olecon);
                    DataTable dt = new DataTable("image");        OleDbDataAdapter oda = new OleDbDataAdapter(olecmd);
            
            ds.Tables.Add(dt);        oda.Fill(ds,"image");        this.TextBox1.Text = ds.Tables["image"].Rows[0][1].ToString();        this.img1.ImageUrl = this.TextBox1.Text.ToString();
           
            
        }
        protected void Command1_Click(object sender, EventArgs e)
        {
            
         
        }
      

  3.   

    把DataSet ds = new DataSet();
    移到Page_Load{}外面啊。
      

  4.   

    好像移到外边的话,如果你不定义为static的,那么在page_load()函数里不要加
    if(!this.ispostback).