本文介绍了aspx.cs 文件中变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写以下代码:
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:UsersjayantDocumentsUser_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
}
}
}
这里我的 DataSet 变量是全局变量,但是当我单击 button2 时,它不会向输出 XML 文件发送任何数据.你能告诉我为什么吗?或者我应该做哪些修改来执行此操作?
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?
谢谢
推荐答案
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersjayantDocumentsUser_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 文件中变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!