Scope of Object Variables
-
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}
}
-
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}
}
-
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}
}
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.
-
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.
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
-
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 :-)
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
-
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
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.