Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Thursday, March 29, 2012

Session variable

Hi Team,

I am using one Session variable to passing data between method in one page. For example: I am using Session["SourceType"] variable, which the value always changing depends on where the user want to view the content.

My question is, when I store another value into the same session variable (Session["SourceType"]) name, do I need to delete/remove first the old value or it will just overwritten the old value.

I hope that my question is clear and thanks in advance.

You can simply overwrite the old value when using Session variables. Just remember to cast it to proper type when reading it back.
It will overwrite it.

it will overwrite the data if used like Session["sourceType"] = "new value"

however if you have the SourceType variable set and try to go

Session.Add("sourceType","value"); it will cause an error.

hth,

mcm


Thanks team,

So it means that the old value will be removed right. Because I don't want the old value just floating around the applications.

Thanks.


yes,

the .net Garbage Collector will remove the old value from memory when you assign the same variable another value.

mcm

Monday, March 26, 2012

Session variable in static method creates error.

Hi. I have a static method that adds a number to a session variable. But when I code a line like this:

Session["CartSubTotal"] = Convert.ToDouble(Session["CartSubTotal"]) + Price;

I get an error like this:

c:\inetpub\wwwroot\aaagolfballs\ShoppingCart.aspx.cs(235): An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session'

What is a guy to do?If Session["CartSubTotal"] has not been set, that is the error you get.

Check if Session["CartSubTotal"]==null, if so, just set it equal to price rather than trying to add in the existing value of the session variable...
It was null, but I put into the global.asax:


protected void Session_Start(Object sender, EventArgs e)
{
Session["CartSubTotal"] = 0;
}

I still get the same error.
I've got it. For those with the same error, here is how I fixed it. I used these variables in the static method:
System.Web.HttpContext.Current.Session
System.Web.HttpContext.Current.Server

Thanks to all.

Session variable not set

Hi All,
I built a web application using cookieless session. It merely checks
for the session variable value upon login. This method works fine on
the host pc.
However, when i try to access the application via another pc in the
network, the login page redirects to itself even though the correct
passwd and usename is entered. I did a response.write and realise that
the value of the session variable is not set. How can i solve this
prob?
Any Help is greatly appreciated.
Tks...ywzCheck out http://weblogs.asp.net/bleroy/archi.../03/207486.aspx
it might solve your problem...
Karl
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"ywz" <ywz.oct13@.gmail.com> wrote in message
news:1113293239.075593.241860@.o13g2000cwo.googlegroups.com...
> Hi All,
>
> I built a web application using cookieless session. It merely checks
> for the session variable value upon login. This method works fine on
> the host pc.
> However, when i try to access the application via another pc in the
> network, the login page redirects to itself even though the correct
> passwd and usename is entered. I did a response.write and realise that
> the value of the session variable is not set. How can i solve this
> prob?
>
> Any Help is greatly appreciated.
> Tks...ywz
>
The fact that it works on one and not the other means this is probably not
the answer, although this is a great post.
The major problem, IMO, is the fact that ywz is using an ASP methodology
(session var) in an ASP.NET world.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Karl Seguin" wrote:

> Check out http://weblogs.asp.net/bleroy/archi.../03/207486.aspx
> it might solve your problem...
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
>
> "ywz" <ywz.oct13@.gmail.com> wrote in message
> news:1113293239.075593.241860@.o13g2000cwo.googlegroups.com...
>
>

Session variable not set

Hi All,

I built a web application using cookieless session. It merely checks
for the session variable value upon login. This method works fine on
the host pc.
However, when i try to access the application via another pc in the
network, the login page redirects to itself even though the correct
passwd and usename is entered. I did a response.write and realise that
the value of the session variable is not set. How can i solve this
prob?

Any Help is greatly appreciated.
Tks...ywzCheck out http://weblogs.asp.net/bleroy/archi.../03/207486.aspx
it might solve your problem...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)

"ywz" <ywz.oct13@.gmail.com> wrote in message
news:1113293239.075593.241860@.o13g2000cwo.googlegr oups.com...
> Hi All,
>
> I built a web application using cookieless session. It merely checks
> for the session variable value upon login. This method works fine on
> the host pc.
> However, when i try to access the application via another pc in the
> network, the login page redirects to itself even though the correct
> passwd and usename is entered. I did a response.write and realise that
> the value of the session variable is not set. How can i solve this
> prob?
>
> Any Help is greatly appreciated.
> Tks...ywz
The fact that it works on one and not the other means this is probably not
the answer, although this is a great post.

The major problem, IMO, is the fact that ywz is using an ASP methodology
(session var) in an ASP.NET world.

--

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Karl Seguin" wrote:

> Check out http://weblogs.asp.net/bleroy/archi.../03/207486.aspx
> it might solve your problem...
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
>
> "ywz" <ywz.oct13@.gmail.com> wrote in message
> news:1113293239.075593.241860@.o13g2000cwo.googlegr oups.com...
> > Hi All,
> > I built a web application using cookieless session. It merely checks
> > for the session variable value upon login. This method works fine on
> > the host pc.
> > However, when i try to access the application via another pc in the
> > network, the login page redirects to itself even though the correct
> > passwd and usename is entered. I did a response.write and realise that
> > the value of the session variable is not set. How can i solve this
> > prob?
> > Any Help is greatly appreciated.
> > Tks...ywz
>

Thursday, March 22, 2012

session Variables

ok once you have set a session variable i was wondering wich is the preffered method used to cast it to say a textbox

for examples

tbCity.Text = (string)Session["City"];

or

tbCity.Text = Session["City"].ToString();

would like ot know wich is the preffered method and why?

ilphrin:

tbCity.Text = (string)Session["City"];

I prefer your first way because you are being explicit about what the type is. otherwise, you are assuming it is an object that implements ToString(). Actually, I'm surprised that even works. I did not know Object has a ToString().


I THINK, HERE IS A DIFFERENCE

tbCity.Text =Session["City"].ToString(); Copies the value Session["City"] has into tbCity.Text
tbCity.Text = (string)Session["City"]; Trys to cast Session["City"] into a string object and copy a pointer


ilphrin:

I prefer your first way because you are being explicit about what the type is. otherwise, you are assuming it is an object that implements ToString().

All objects in .NET implement ToString() so that is not the problem.

farooq.kaiser:

I THINK, HERE IS A DIFFERENCE

tbCity.Text =Session["City"].ToString(); Copies the value Session["City"] has into tbCity.Text
tbCity.Text = (string)Session["City"]; Trys to cast Session["City"] into a string object and copy a pointer

No, it's by value (not a pointer) in both cases.

The technical difference is that ToString() will call the (hopefully) overridden ToString method on the object in Session["City"], whereas the (string)version tries to cast the object (may fail) to a string.

Problems with ToString - The object may be null, and you will get an "object reference not set" exception.
Problem with cast - The object may not be castable to string

Alternative solution:

tbCity.Text = Convert.ToString(Session["City"]);

Convert will use ToString internally, but if Session["City"] is null, it will instead return an empty string (unlike the cast, which will return a null string)

I usually use the following syntax:

Session["ObjectList"] as ObjectListType;

The *as* keyword ensures that if the casting fails there will be no error but a null value is returned.


no i am just wondering wich will be a preffered way of doing it as in wich way uses less overhead, and perhaps is a tad bit faster to execute

the main thing is i can get around a null value for ToString by using an if (Session["City"] != null)

and then putting it into the textbox. basically it is what is the best choice in the area of overhead, could be using literally hundreds of these iterated thru and want the fastest execution.


Performance wise they are virtually identical (perhaps a small, small edge to ToString() but checking for null would most likely eat that difference). I'm quite sure you have other areas of optimization
which will give you differences that are magnitudes larger than the difference between these, even if you have tens of thousands of records.


I am agree withgunteman you should always use Convert.ToString(your object)

Convert provide a lot of method like ToInt etc. just have a look.

Session Variables

Hey Tibby.

The proper way to create a session variable is:

Session["VarName"] = value;

Or you can use the Add method as:

Session.Add("VarName", value);

Either way will work.

Next to remove an item from the session state:

Session.Remove("VarName");

To clear all items from the session state use:

Session.Clear();

HTH.

Jeff Ptak

>--Original Message--
>What is the proper way to creating Session Variables and
destroying them. I
>think this is what my major problem is. And, can that be
done also for
>ADO.NET objects?
>Thanks
>Tibby
>
>.Just to make sure I'm playing ball here, so, instead of using Dim myVar as
string, then later using myVar="Kiss Me", I would use Session["myVar"]="Kiss
Me", then to reference it, I would use something like
MsgBox(Session["myVar"])
Just to make sure I'm on the same page.

Thanks
Tibby

"J. Ptak" <ptakja@.NOSPAM.corning.com> wrote in message
news:8eb301c35b69$62ce8060$a001280a@.phx.gbl...
> Hey Tibby.
> The proper way to create a session variable is:
> Session["VarName"] = value;
> Or you can use the Add method as:
> Session.Add("VarName", value);
> Either way will work.
> Next to remove an item from the session state:
> Session.Remove("VarName");
> To clear all items from the session state use:
> Session.Clear();
> HTH.
> Jeff Ptak
>
> >--Original Message--
> >What is the proper way to creating Session Variables and
> destroying them. I
> >think this is what my major problem is. And, can that be
> done also for
> >ADO.NET objects?
> >Thanks
> >Tibby
> >.
I apoligize, I'm using VB.Net.

"Kevin Spencer" <kevin@.takempis.com> wrote in message
news:OaUHXi3WDHA.1620@.TK2MSFTNGP12.phx.gbl...
> Forget about "MsgBox." That function is not for ASP.Net pages. It is only
> used for Windows Forms. Other than that, you are basically corrrect.
> However, it is hard to tell whether you're using VB.Net or C# from your
> example, as the MsgBox function is a VB.Net function, but your syntax for
> the Session variable is C#. Here are 2 examples, using VB.Net and C#:
> VB.Net: Session("myVar") = "Kiss Me"
> C#: Session["myVar"] = "Kiss Me";
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> http://www.takempis.com
> Complex things are made up of
> lots of simple things.
> "Tibby" <tierscheiss1977@.hotmail.com> wrote in message
> news:uM5PwY3WDHA.2568@.tk2msftngp13.phx.gbl...
> > Just to make sure I'm playing ball here, so, instead of using Dim myVar
as
> > string, then later using myVar="Kiss Me", I would use
> Session["myVar"]="Kiss
> > Me", then to reference it, I would use something like
> > MsgBox(Session["myVar"])
> > Just to make sure I'm on the same page.
> > Thanks
> > Tibby
> > "J. Ptak" <ptakja@.NOSPAM.corning.com> wrote in message
> > news:8eb301c35b69$62ce8060$a001280a@.phx.gbl...
> > > Hey Tibby.
> > > > The proper way to create a session variable is:
> > > > Session["VarName"] = value;
> > > > Or you can use the Add method as:
> > > > Session.Add("VarName", value);
> > > > Either way will work.
> > > > Next to remove an item from the session state:
> > > > Session.Remove("VarName");
> > > > To clear all items from the session state use:
> > > > Session.Clear();
> > > > HTH.
> > > > Jeff Ptak
> > > > > >--Original Message--
> > > >What is the proper way to creating Session Variables and
> > > destroying them. I
> > > >think this is what my major problem is. And, can that be
> > > done also for
> > > >ADO.NET objects?
> > > > > >Thanks
> > > >Tibby
> > > > > > > >.
> >
No apology neccessary! That's what we're here for. :)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.

"Tibby" <tierscheiss1977@.hotmail.com> wrote in message
news:OKRSgL4WDHA.2268@.TK2MSFTNGP11.phx.gbl...
> I apoligize, I'm using VB.Net.
> "Kevin Spencer" <kevin@.takempis.com> wrote in message
> news:OaUHXi3WDHA.1620@.TK2MSFTNGP12.phx.gbl...
> > Forget about "MsgBox." That function is not for ASP.Net pages. It is
only
> > used for Windows Forms. Other than that, you are basically corrrect.
> > However, it is hard to tell whether you're using VB.Net or C# from your
> > example, as the MsgBox function is a VB.Net function, but your syntax
for
> > the Session variable is C#. Here are 2 examples, using VB.Net and C#:
> > VB.Net: Session("myVar") = "Kiss Me"
> > C#: Session["myVar"] = "Kiss Me";
> > --
> > HTH,
> > Kevin Spencer
> > Microsoft MVP
> > .Net Developer
> > http://www.takempis.com
> > Complex things are made up of
> > lots of simple things.
> > "Tibby" <tierscheiss1977@.hotmail.com> wrote in message
> > news:uM5PwY3WDHA.2568@.tk2msftngp13.phx.gbl...
> > > Just to make sure I'm playing ball here, so, instead of using Dim
myVar
> as
> > > string, then later using myVar="Kiss Me", I would use
> > Session["myVar"]="Kiss
> > > Me", then to reference it, I would use something like
> > > MsgBox(Session["myVar"])
> > > Just to make sure I'm on the same page.
> > > > Thanks
> > > Tibby
> > > > "J. Ptak" <ptakja@.NOSPAM.corning.com> wrote in message
> > > news:8eb301c35b69$62ce8060$a001280a@.phx.gbl...
> > > > Hey Tibby.
> > > > > > The proper way to create a session variable is:
> > > > > > Session["VarName"] = value;
> > > > > > Or you can use the Add method as:
> > > > > > Session.Add("VarName", value);
> > > > > > Either way will work.
> > > > > > Next to remove an item from the session state:
> > > > > > Session.Remove("VarName");
> > > > > > To clear all items from the session state use:
> > > > > > Session.Clear();
> > > > > > HTH.
> > > > > > Jeff Ptak
> > > > > > > > >--Original Message--
> > > > >What is the proper way to creating Session Variables and
> > > > destroying them. I
> > > > >think this is what my major problem is. And, can that be
> > > > done also for
> > > > >ADO.NET objects?
> > > > > > > >Thanks
> > > > >Tibby
> > > > > > > > > > >.
> > > > >