Showing posts with label integer. Show all posts
Showing posts with label integer. Show all posts

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.

Tuesday, March 13, 2012

Strange problem

I have a class method which takes integer as parameter...when i call the
method as shown below i get this error message: Specified cast is not valid
<td><%# bs.getPriority((int)DataBinder.Eval(Container.DataItem,
"Priority"))%></td>
When i print the DataItem "Priority" it shows an integer value, but for some
reason the method does not like value.. if i call this method like ->
bs.getPriortiy(3); it works fine.
In the database i have a field called "Priority" int type...
The method works fine when i call it in DataItemBound method..
any idea?Hi,
Try using
int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
DataBinder.Eval returns a string value type. (int) is used to
implicitly cast values to integers. Strings must be explicitly cast
using the Parse method.
Hope this helps.
Tod Birdsall, MCP
http://tod1d.blogspot.com
Tod thank you so much :) it worked..
still why one of them works not the other as shown below:
<TD><%# bs.getCampusName((int)DataBinder.Eval(Container.DataItem,
"CampusID"))%></TD>
<td><%# bs.getCampusName((int.Parse(DataBinder.Eval(Container.DataItem,
"Priority")))%></td>
both CampusID and Priority is same type of value..
Many Thanks again.
"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Sorry that did not solve the problem... :( i am getting this error message
now:
The best overloaded method match for 'int.Parse(string)' has some invalid
arguments
"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Acutally, DataBinder.Eval returns an object unless you pass a third
parameter, a format specifier, i.e.
DataBinder.Eval(Container.DataItem, "Priority", "{0:d}")
Otherwise you get an object reference back, in which case you could
use Convert.ToInt32
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 9 Nov 2004 07:38:03 -0800, "huzz"
<huzz@.discussions.microsoft.com> wrote:
>Sorry that did not solve the problem... :( i am getting this error message
>now:
>The best overloaded method match for 'int.Parse(string)' has some invalid
>arguments
>"Tod Birdsall" wrote:
>
Hi,
Scott is correct. Sorry for the mis-information.
I tried to reproduce your error message. I created a function in the
code behind like so:
protected string GetRemainderInEscrow(decimal approvedAmount, object
paidAmount)
I then called the method from the aspx page using the following code :
<%# GetRemainderInEscrow( (decimal)DataBinder.Eval(Container,
"DataItem.ApprovedAmount"), DataBinder.Eval(Container, "DataItem.Paid")
) %>
The code worked as it should. If you have control over the
'bs.getPriority' method, try allowing an object to be passed instead of
an integer value type. If that doesn't work. If that still does not
work, copy and paste the exact error message and Stack Trace.
Tod Birdsall, MCP
http://tod1d.blogspot.com

Strange problem

I have a class method which takes integer as parameter...when i call the
method as shown below i get this error message: Specified cast is not valid

<td><%# bs.getPriority((int)DataBinder.Eval(Container.Data Item,
"Priority"))%></td
When i print the DataItem "Priority" it shows an integer value, but for some
reason the method does not like value.. if i call this method like ->
bs.getPriortiy(3); it works fine.

In the database i have a field called "Priority" int type...

The method works fine when i call it in DataItemBound method..

any idea?Hi,

Try using

int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))

DataBinder.Eval returns a string value type. (int) is used to
implicitly cast values to integers. Strings must be explicitly cast
using the Parse method.
Hope this helps.

Tod Birdsall, MCP
http://tod1d.blogspot.com
Tod thank you so much :) it worked..

still confused why one of them works not the other as shown below:
<TD><%# bs.getCampusName((int)DataBinder.Eval(Container.Da taItem,
"CampusID"))%></TD
<td><%# bs.getCampusName((int.Parse(DataBinder.Eval(Contai ner.DataItem,
"Priority")))%></td
both CampusID and Priority is same type of value..

Many Thanks again.

"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Sorry that did not solve the problem... :( i am getting this error message
now:

The best overloaded method match for 'int.Parse(string)' has some invalid
arguments

"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Acutally, DataBinder.Eval returns an object unless you pass a third
parameter, a format specifier, i.e.

DataBinder.Eval(Container.DataItem, "Priority", "{0:d}")

Otherwise you get an object reference back, in which case you could
use Convert.ToInt32

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 9 Nov 2004 07:38:03 -0800, "huzz"
<huzz@.discussions.microsoft.com> wrote:

>Sorry that did not solve the problem... :( i am getting this error message
>now:
>The best overloaded method match for 'int.Parse(string)' has some invalid
>arguments
>"Tod Birdsall" wrote:
>> Hi,
>>
>> Try using
>>
>> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
>>
>> DataBinder.Eval returns a string value type. (int) is used to
>> implicitly cast values to integers. Strings must be explicitly cast
>> using the Parse method.
>> Hope this helps.
>>
>> Tod Birdsall, MCP
>> http://tod1d.blogspot.com
>>
>
Hi,

Scott is correct. Sorry for the mis-information.

I tried to reproduce your error message. I created a function in the
code behind like so:

protected string GetRemainderInEscrow(decimal approvedAmount, object
paidAmount)

I then called the method from the aspx page using the following code :

<%# GetRemainderInEscrow( (decimal)DataBinder.Eval(Container,
"DataItem.ApprovedAmount"), DataBinder.Eval(Container, "DataItem.Paid")
) %
The code worked as it should. If you have control over the
'bs.getPriority' method, try allowing an object to be passed instead of
an integer value type. If that doesn't work. If that still does not
work, copy and paste the exact error message and Stack Trace.
Tod Birdsall, MCP
http://tod1d.blogspot.com