Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Scope of Object Variables

Scope of Object Variables

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesignhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yogesh_kumar_agarwal
    wrote on last edited by
    #1

    Hi, All I just start a new program i asp.net and it confuse me in some basic concept. as you can see i just create a public class named connection then i want to initialize it on page load. but when ever i click on any of the button, it give me the error null reference error whats wrong in ASP.NET, because same work with the windows application with C#. plz do reply regards Yogesh Agarwal

    public partial class Admin_Default : System.Web.UI.Page
    {
    Connection con;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    con = new Connection();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    con.skysconnection();
    //some task
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    con.skysconnection();
    //some task

    }
    

    }

    S S 2 Replies Last reply
    0
    • Y yogesh_kumar_agarwal

      Hi, All I just start a new program i asp.net and it confuse me in some basic concept. as you can see i just create a public class named connection then i want to initialize it on page load. but when ever i click on any of the button, it give me the error null reference error whats wrong in ASP.NET, because same work with the windows application with C#. plz do reply regards Yogesh Agarwal

      public partial class Admin_Default : System.Web.UI.Page
      {
      Connection con;
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      con = new Connection();
      }
      protected void Button1_Click(object sender, EventArgs e)
      {
      con.skysconnection();
      //some task
      }
      protected void Button2_Click(object sender, EventArgs e)
      {
      con.skysconnection();
      //some task

      }
      

      }

      S Offline
      S Offline
      simsen
      wrote on last edited by
      #2

      Hi, Why do you make your connection int he Page_Load? I cannot see the reason for doing this. Why not in the skysconnection() method? Kind regards simsen :-)

      Y 1 Reply Last reply
      0
      • Y yogesh_kumar_agarwal

        Hi, All I just start a new program i asp.net and it confuse me in some basic concept. as you can see i just create a public class named connection then i want to initialize it on page load. but when ever i click on any of the button, it give me the error null reference error whats wrong in ASP.NET, because same work with the windows application with C#. plz do reply regards Yogesh Agarwal

        public partial class Admin_Default : System.Web.UI.Page
        {
        Connection con;
        protected void Page_Load(object sender, EventArgs e)
        {
        if (!IsPostBack)
        con = new Connection();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
        con.skysconnection();
        //some task
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
        con.skysconnection();
        //some task

        }
        

        }

        S Offline
        S Offline
        Samer Aburabie
        wrote on last edited by
        #3

        ASP.NET is stateless ... which means it doesnt maintain the state of its objects ... on each request and response cycle the page is treated as something new and for the first time ... thats why state management in ASP.NET is a challenge ... The code you provided should work on Windows Application because it maintains the state of its object ... but here each time you click the button the page is reinitialized with its variables you declared ..to solve such a problem read about maintaining objects states using Sessions and ViewStates in ASP.NET

        Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

        Y 1 Reply Last reply
        0
        • S Samer Aburabie

          ASP.NET is stateless ... which means it doesnt maintain the state of its objects ... on each request and response cycle the page is treated as something new and for the first time ... thats why state management in ASP.NET is a challenge ... The code you provided should work on Windows Application because it maintains the state of its object ... but here each time you click the button the page is reinitialized with its variables you declared ..to solve such a problem read about maintaining objects states using Sessions and ViewStates in ASP.NET

          Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

          Y Offline
          Y Offline
          yogesh_kumar_agarwal
          wrote on last edited by
          #4

          Thanks for the answer, Means in every pageload or postback the objects were destroyed we defined in ASP.NET, And does it works fine if i use ajax. Regards Yogesh Agarwal

          S 1 Reply Last reply
          0
          • S simsen

            Hi, Why do you make your connection int he Page_Load? I cannot see the reason for doing this. Why not in the skysconnection() method? Kind regards simsen :-)

            Y Offline
            Y Offline
            yogesh_kumar_agarwal
            wrote on last edited by
            #5

            Thanks for the reply. Acctually it is just a doubt in the mind, i could do it in sevral manners, i just want to know what is the problem in doing the same in this manner. Regards Yogesh Agarwal

            1 Reply Last reply
            0
            • Y yogesh_kumar_agarwal

              Thanks for the answer, Means in every pageload or postback the objects were destroyed we defined in ASP.NET, And does it works fine if i use ajax. Regards Yogesh Agarwal

              S Offline
              S Offline
              Samer Aburabie
              wrote on last edited by
              #6

              Actually AJAX has nothing to do with the main life cycle of a page ... for the scope you are talking about its only the server side .. and there where you are concerned about the life cycle of page.

              Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups