Wednesday, March 28, 2012

Strange behaviour of Integers and Strings ?

If I am declaring a variable as integer ( int intcv_id = 0;) at the top of my page, so that i can access the value in all the methods thats my idea.

if(!IsPostBack)
{
BindData();
}

In one of my methods e.g. BindData() , I have changed the value of variable (e.g.intcv_id =Convert.ToInt32(Request.QueryString["id"]), incv_id is working properly in the BindData() method but when I am trying access the intcv_id value in some other methods, its just giving the initial value 0. I know that I can use Sessions to get over this problem, but I just want to know is there any other way out, rather than using sessions/viewstate.

Why is the integer behaving like that ? Integer is a Value-type isn't, it directly stores the value in the memory rather than its reference. Then why can't i access the stored memory value?

Its same with the strings i couldn't access the string value other than in its method , though I declare it globally. why are the value types and reference types behaving like this ?

Can anybody tell me whats happening inside please, these are basic doubts I am having though I am coding since a while

Thanks in Advance

When are you calling BindData relative to your referencing intcv_id elsewhere?

Asp.net is stateless, unless you store the value in ViewState/Session the integer variable will loose its value on postback.

I suggest create a property, each time you need the Id from the query string get it from the property.

public string Id

{

get { return Convert.ToInt32(Request.QueryString["id"]); }

}

Hope this answers your question.


Asp.net is stateless, unless you store the value in ViewState/Session the integer variable will loose its value on postback.

I suggest create a property, each time you need the Id from the query string get it from the property.

public string Id

{

get { return Convert.ToInt32(Request.QueryString["id"]); }

}

Hope this answers your question.


This is just a part of code i have edited it for readability, hope it makes sense, Thanks for your time
public partialclass viewcv : System.Web.UI.Page{SqlConnection objConn =new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DSN"]);SqlCommand objcmd;SqlDataReader objRdr;int intcv_id = 0;string js_id ="";protected void Page_Load(object sender, System.EventArgs e){if(!IsPostBack){BindData();}}private void BindData(){if(Convert.ToString(Request.QueryString["id"])!=null){ objcmd =new SqlCommand("SelectCreateCVDetails", objConn); objcmd.CommandType = CommandType.StoredProcedure; objcmd.Parameters.Add("@.cv_id", Request.QueryString["id"]); objConn.Open(); objRdr= objcmd.ExecuteReader(); objRdr.Read(); js_id = Convert.ToString(objRdr["JS_ID"]); objRdr.Close(); intcv_id = Convert.ToInt32(Request.QueryString["id"]); }}protected void lbcv_Click(object sender, EventArgs e) { Response.Write(js_id); // here if i test its giving null which is suppose to be 29 Response.Write(intcv_id); // here its suppose to be 17 but its 0 } }
 
 

Based on this sample it appears that you need to call DataBind every time (remove the PostBack condition). The Click event can only occur during a PostBack and you're only setting the values during the initial load.

Thank you very much guys for your help.

Both the property method and by removing the !IsPostBack in the Page_Load are working great.

So, as its storing the values initially but not during the postback and as the webpages are stateless we need to store them on every Page_Load , hope I am right in my deduction.

Thank you very much guys for your help and time


There are ways to store them, but it's usually more appropriate to only load when needed and if needed on every page instantiation then in Page_Load.

0 comments:

Post a Comment