本文介绍了在aspx.cs文件中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写以下code:

 命名空间WebApplication5
{
    公共部分类WebForm1的:System.Web.UI.Page
    {
        私人数据集数据集1 =新的DataSet();
        OleDbDataAdapter的适配器;        公开数据集DS
        {
            {返回数据集1; }
            集合{数据集1 =值; }
        }        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
        }        保护无效的button1_Click(对象发件人,EventArgs的发送)
        {
            OleDbConnection的CON =新的OleDbConnection();
            con.ConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\\用户\\贾扬特·\\文档\\ User_Details.accdb
            con.Open();
            适配器=新OleDbDataAdapter的(选择User_Details *,CON);
            adapter.Fill(DS);
            GridView1.DataSource = DS;
            GridView1.DataBind();
           // ds.WriteXml(C:\\\\ MyUser_Details.xml);如果我这样做,在这里写入数据
            con.Close();
        }        保护无效Button2_Click(对象发件人,EventArgs的发送)
        {
            ds.WriteXml(C:\\\\ MyUser_Details.xml);
            //在XML文件中没有任何数据,只是根标签
        }
    }
}

下面我的数据集的变量是全球,但是当我点击按钮2,它没有数据发送到输出继电器XML文件。你能告诉我为什么?或者,我应该做出怎样的修改来执行此?

感谢


解决方案

 保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        OleDbConnection的CON =新的OleDbConnection();
        con.ConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\\用户\\贾扬特·\\文档\\ User_Details.accdb
        con.Open();
        适配器=新OleDbDataAdapter的(选择User_Details *,CON);
        adapter.Fill(DS);
        GridView1.DataSource = DS;
        Session.Add(数据集,DS); //添加会话这里
        GridView1.DataBind();
        con.Close();
    }    保护无效Button2_Click(对象发件人,EventArgs的发送)
    {
        数据集DS =(数据集)会话[数据集]; //获取来自会话值
        ds.WriteXml(C:\\\\ MyUser_Details.xml);
    }

I am writing following code:

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private DataSet dataset1 = new DataSet();
        OleDbDataAdapter adapter;

        public DataSet ds
        {
            get { return dataset1; }
            set { dataset1 = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jayant\Documents\User_Details.accdb";
            con.Open();
            adapter = new OleDbDataAdapter("Select * from User_Details",con);
            adapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
           //  ds.WriteXml("C:\\MyUser_Details.xml"); If I do this here it writes data
            con.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ds.WriteXml("C:\\MyUser_Details.xml");
            // no data in xml files, just root tags
        }
    }
}

Here my DataSet variable is global but when I am clicking on button2, it sends no data to the Ouput XML file. Can you please tell me why? Or what modifications shall I make to perform this?

Thanks

解决方案
 protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jayant\Documents\User_Details.accdb";
        con.Open();
        adapter = new OleDbDataAdapter("Select * from User_Details",con);
        adapter.Fill(ds);
        GridView1.DataSource = ds;
        Session.Add("Dataset", ds); //Adding Session here
        GridView1.DataBind();
        con.Close();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["Dataset"]; //Retrieving Value from session
        ds.WriteXml("C:\\MyUser_Details.xml");
    }

这篇关于在aspx.cs文件中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:02