Scope of variables in aspx.cs file

后端 未结 6 2077
感情败类
感情败类 2021-01-29 04:15

I am writing following code:

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private DataSet dataset1 = new Data         


        
相关标签:
6条回答
  • 2021-01-29 04:49

    It is because you create a new Dataset during postback.

    move the initialization of the Dataset private DataSet dataset1= new DataSet();

    in to the Load

    protected void Page_Load(object sender, EventArgs e)
        {
             if(!Page.IsPostBack) { dataset1= new DataSet(); }
        }
    

    and obviously, don`t forget to click Button1 before you click Button2 :)

    0 讨论(0)
  • 2021-01-29 04:53

    Yes you are partially correct that your ds is Global but only for life time of Web Page. Once the page is rendered and sent to client, your page is disposed and hence with it the variable.

    If you want to ds to be available in button_click event, either populate it inside Page_Load event

    protected void Page_Load(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);
                con.close();
    }
    

    Or generate the dataset in your Button_Click event handler.

    If you do not want to generate this data set every time, you have to either keep this variable in Session or ViewState or Cache

    UPDATE 1

    The third and last option could be to make this dataset variable as class level variable i.e static and it would be available all the time

    0 讨论(0)
  • 2021-01-29 04:54
     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");
        }
    
    0 讨论(0)
  • 2021-01-29 04:57

    You need to have a look at Asp.net page life cycle, In your code when you click on your button page is destroyed and re created again and whole life cycle is followed and in that process your ds(dataset) is recreated too, therefore output xml doesn't have any data in it. To maintain state of your dataset have a look at State Management in Asp.net

    0 讨论(0)
  • 2021-01-29 05:04

    You don't get Data in the dataset in button2_click because button1_click and button2_clicks are entirely different requests to the server. And asp.net does not persists data stored in variables between requests. If you need to persist data you should use any State Management techniques provided by asp.net, like Session or Viewstate or Caching.

    0 讨论(0)
  • 2021-01-29 05:14

    I suggest please use ViewState as Session will consume lot of server memory, though you can use any one of them to solve the purpose.

    I don't think button1's event handler is doing anything apart from creating ds. So only one event handler will solve the purpose.

    Another way to access ds in both the event handlers without losing data is declare ds as static. But this mechanism does not suit this scenario.

    0 讨论(0)
提交回复
热议问题