Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Thursday, March 29, 2012

Session variable - no cookies

Hi;

I need to keep a string in Session["name"] however this does not work if the
user has cookies turned off. How can I work around this?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htmYou should use cookieless session... Then you do not have to worry about
user cookie settings.
asp.net needs browser cookie settings to be turned on to store the session
ID, which is the default setting.. But if you go into the web.config and set
cookieless=true, then asp.net will embed the session id in the url and will
make your application independent of browser cookie settings.

"David Thielen" <thielen@.nospam.nospamwrote in message
news:11A2BCCE-2E10-40C9-A0F7-18DFE6DB79C0@.microsoft.com...

Quote:

Originally Posted by

Hi;
>
I need to keep a string in Session["name"] however this does not work if
the
user has cookies turned off. How can I work around this?
>
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
>
Cubicle Wars - http://www.windwardreports.com/film.htm
>
>


Hi;

Reading this it looks like you are either always cookieless or always
cookie(?) but can't have it go cookieless only if necessary. I'd rather not
have the cookie always in the url for the occasional paranoid who doesn't
allow session cookies.

So...

Is there a way to determine if cookies are allowed and put up some text in
the web page if they are turned off?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
"Kumar Reddi" wrote:

Quote:

Originally Posted by

You should use cookieless session... Then you do not have to worry about
user cookie settings.
asp.net needs browser cookie settings to be turned on to store the session
ID, which is the default setting.. But if you go into the web.config and set
cookieless=true, then asp.net will embed the session id in the url and will
make your application independent of browser cookie settings.
>
"David Thielen" <thielen@.nospam.nospamwrote in message
news:11A2BCCE-2E10-40C9-A0F7-18DFE6DB79C0@.microsoft.com...

Quote:

Originally Posted by

Hi;

I need to keep a string in Session["name"] however this does not work if
the
user has cookies turned off. How can I work around this?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm


>
>
>


Hi David,

You are right that you will have to be always cookieless or always cookie.
Setting cookieless="AutoDetect" will only use the browser capabilities, if
user disabled cookies, cookies still will be used by not working.

One way to determine whether cookies are accepted is by trying to write a
cookie and then trying to read it back again. If you cannot read the cookie
you wrote, you assume that cookies are turned off in the browser.

For complete code listing, please see following MSDN Library:

#ASP.NET Cookies Overview
http://msdn2.microsoft.com/en-us/library/ms178194.aspx
(Search for section titled "Determining Whether a Browser Accepts Cookies")

Hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang (wawang@.online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Monday, March 26, 2012

Session variable concatenation

Hi,
I think I might have misunderstood the implementation of this concept in ASP.NET/C#.
I have a string array called WinArr[]. I would like to concat the contents of three session variables and put the result into WinArr[0]; I have tried:

WinArr[0] =this.Session["WinBoxesArr0"].ToString() +this.Session["WinBoxesArr1"].ToString() +this.Session["WinBoxesArr2"].ToString();

but I get the following error (with or without using this. ) :

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 70: {Line 71:Line 72: WinArr[0] = this.Session["WinBoxesArr0"].ToString() + this.Session["WinBoxesArr1"].ToString() + this.Session["WinBoxesArr2"].ToString();Line 73:


Any help appreciated...

E.Have you properly defined the array?

string[] WinArr =newstring[1];


Have you instantiated theWinArr? Also, are you sure that each Session variable has been set?

Thanks! I forgot to set the variables...
This looks like the same problems you brought up inhttp://forums.asp.net/1079383/ShowPost.aspx with Application replaced by Session.
Sort of, but not really. The original problem was with arrays.

And this problem looks like it's with arrays also. Try this:
string [] WinArr = new string [1];

WinArr[0] = string.Empty;

if ( this.Session["WinBoxesArr0"] != null)
WinArr[0] += this.Session["WinBoxesArr0"].ToString();
if ( this.Session["WinBoxesArr1"] != null)
WinArr[0] += this.Session["WinBoxesArr0"].ToString();
if ( this.Session["WinBoxesArr2"] != null)
WinArr[0] += this.Session["WinBoxesArr0"].ToString();
NC...

session variable or object

i have some problem with session object:

for example if i define a session variable
and make it equal to a string(i.e session("str")="fgjfkg" )
when i want to use it in another page it's value is equal with nothing
(i.e session("str")=nothing)
is you know how to solve prolem please tell me

thanksHello, make sure session are enabled in the asp.net application you have. Check your web.config file.
Also, make sure that cookies are enabled on ur machine, since sessions will use cookies.

regards.
hi

thanks for your suggestion,i try it but it didn,t
solve my problem again the session variable couldn't
store any value i have a question is it neccesary to use it
in "session start" in clobal.asax

regards
A.Doroudian
Hello, yes you should, you have to inclide the Session_Onstart method.

regards.

Saturday, March 24, 2012

Session Variable update problem

I have two ASPX in my testproject :
1:
In the Page_load there is :
If Not Me.IsPostBack Then
Dim tfm
tfm = 30
Session("tfm") = tfm
Dim popupScript As String = "<script
language='javascript'>alert('Yes');</script>"
Page.RegisterStartupScript("PopupScript", popupScript)
End If
and there is the function that should change to value of tfm
Private Sub rlist1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles rlist1.CheckedChanged
Dim tfm
tfm = Session("tfm")
tfm = 55
Session("tfm") = tfm
lblTotaal.Text = "gert" & Str(Session("tfm"))
End Sub
What happens : the lblTotaal will show me the 55 instead of the 30. But in
the next aspx the following returns the 30 again
If Not Me.IsPostBack Then
Dim tfm
tfm = Session("tfm")
Label1.Text = Str(tfm)
end if
PLEASE HELP ! What am I doing wrong,Gert:
This may be a ViewState issue, where the view state is changing the value of
the label to the old value. One suggestion would be to set the
EnableViewState property of the label to false.
David Lloyd
MCSD .NET
http://LemingtonConsulting.com
This response is supplied "as is" without any representations or warranties.
"gce" <gceng2@.hotmail.com> wrote in message
news:d5hhqu$7a1$1@.news1.zwoll1.ov.home.nl...
> I have two ASPX in my testproject :
> 1:
> In the Page_load there is :
> If Not Me.IsPostBack Then
> Dim tfm
> tfm = 30
> Session("tfm") = tfm
> Dim popupScript As String = "<script
> language='javascript'>alert('Yes');</script>"
> Page.RegisterStartupScript("PopupScript", popupScript)
> End If
> and there is the function that should change to value of tfm
> Private Sub rlist1_CheckedChanged(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles rlist1.CheckedChanged
> Dim tfm
> tfm = Session("tfm")
> tfm = 55
> Session("tfm") = tfm
> lblTotaal.Text = "gert" & Str(Session("tfm"))
> End Sub
>
> What happens : the lblTotaal will show me the 55 instead of the 30. But in
> the next aspx the following returns the 30 again
> If Not Me.IsPostBack Then
> Dim tfm
> tfm = Session("tfm")
> Label1.Text = Str(tfm)
> end if
> PLEASE HELP ! What am I doing wrong,
>

Session Variable update problem

I have two ASPX in my testproject :

1:

In the Page_load there is :

If Not Me.IsPostBack Then
Dim tfm
tfm = 30
Session("tfm") = tfm
Dim popupScript As String = "<script
language='javascript'>alert('Yes');</script>"
Page.RegisterStartupScript("PopupScript", popupScript)
End If

and there is the function that should change to value of tfm

Private Sub rlist1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles rlist1.CheckedChanged
Dim tfm
tfm = Session("tfm")
tfm = 55
Session("tfm") = tfm
lblTotaal.Text = "gert" & Str(Session("tfm"))
End Sub

What happens : the lblTotaal will show me the 55 instead of the 30. But in
the next aspx the following returns the 30 again

If Not Me.IsPostBack Then
Dim tfm
tfm = Session("tfm")
Label1.Text = Str(tfm)
end if

PLEASE HELP ! What am I doing wrong,Gert:

This may be a ViewState issue, where the view state is changing the value of
the label to the old value. One suggestion would be to set the
EnableViewState property of the label to false.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.

"gce" <gceng2@.hotmail.com> wrote in message
news:d5hhqu$7a1$1@.news1.zwoll1.ov.home.nl...
> I have two ASPX in my testproject :
> 1:
> In the Page_load there is :
> If Not Me.IsPostBack Then
> Dim tfm
> tfm = 30
> Session("tfm") = tfm
> Dim popupScript As String = "<script
> language='javascript'>alert('Yes');</script>"
> Page.RegisterStartupScript("PopupScript", popupScript)
> End If
> and there is the function that should change to value of tfm
> Private Sub rlist1_CheckedChanged(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles rlist1.CheckedChanged
> Dim tfm
> tfm = Session("tfm")
> tfm = 55
> Session("tfm") = tfm
> lblTotaal.Text = "gert" & Str(Session("tfm"))
> End Sub
>
> What happens : the lblTotaal will show me the 55 instead of the 30. But in
> the next aspx the following returns the 30 again
> If Not Me.IsPostBack Then
> Dim tfm
> tfm = Session("tfm")
> Label1.Text = Str(tfm)
> end if
> PLEASE HELP ! What am I doing wrong,

Thursday, March 22, 2012

Session Variables

I want to add session variables (right now i have query string) in my application.

I want to add session variables that can capture the user's userid and email from a table

How do i do that (code? to write?), i tried

Session["username"]=Textbox1.text, on the 1st .aspx page

anD wrote

If Session["username"]!= nullthen

lblname.text =Session["username"].tostring

EndIf

but it doesnt work

Do i have to do changes in the web.config file? if so what changes ? also do i have to write a new /seperate table in the database?

This is the first time i am using session variables ...i dont understand whats exactly i am supposed to do?

By default you can use the Session In-Proc.

You simply need to add this:

Session["KeyName"] = Textbox1.Text;

On another page, simply use this:

if (Session["KeyName"] != null) && (Session["KeyName"].ToString() != "")
{

// Your code goes here
}

It should work without configuring anything in the web application!

Regards


Session by default is stored in memory, so you don't have to do anything.

this syntax is wrong

If Session["username"]!= nullthenlblname.text =Session["username"].tostring

EndIf

the != and null are for C#, in Visual Basic you would do the following

If Not Session("username") Is Nothingthenlblname.Text =Session("username").ToString()

EndIf

edit:corrected the ["..."] to ("...") (VB syntax for collections)


i get this error :property access must assign to property or use its value

You are trying to retreive some property, you are not assigning its value to a variable or using it in an expression, nor are you setting the value of the property. Please post the line of code which has this error.


Session["username"]=Textbox1.text


Session("username")= Textbox1.text

In fact, I've always used Session.Item("username")= Textbox1.text. I didn't realise you could skip the Item


SSpost:

How do i do that (code? to write?), i tried

Session["username"]=Textbox1.text, on the 1st .aspx page

anD wrote

IfSession["username"]!= nullthen

lblname.text =Session["username"].tostring

EndIf

just see above code
which code behind language you are using?

in c#

Session["username"] = TextBox1.Text;

if(Session["username"]!= null)
TextBox1.Text = Session["username"].ToString();

in vb

Session("username") = TextBox1.Text

If Not Session("username") Is Nothing Then
TextBox1.Text = Session("username").ToString()
End If

session variables

Hi, I need to create a session variable in a page that is given a set string value.

In the next page I need to call that variable in the WHERE part of a SELECT clause eg.

SELECT [colA], [colB] WHERE colB = 'value from previous page'

Any help would be greatly appreciated.

Cheers, Chris

VB.NET:

"SELECT [colA], [colB] WHERE colB ='" & Session("Name") & "'"

C#

"SELECT [colA], [colB] WHERE colB ='" + Session["Name"] as string + "'"

HTH,
Ryan


Brilliant, cheers. How do I create the variable in the previous page in VB.NET

Cheers again, Chris


TheCrawsh wrote:

How do I create the variable in the previous page in VB.NET

Session("SomeSession") = SomeValue

Since the value is stored as an object, SomeValue can be of any datatype.

HTH,
Ryan


Cheers, I think I have the syntax wrong, I'm not sure on exactly where to put these, I have this so far:

In the page where the variable is created:

<scriptrunat="server">

Session("banner") ="BSAFD"

</script>

But do I need to put it in a function or something?

In the page that uses the variable:

<asp:SqlDataSourceID="BanRot"runat="server"ConnectionString="<%$ ConnectionStrings:w2gConnectionString %>"

SelectCommand="SELECT [Pic], [Nav], [Page], [Alt] FROM [ads-home] WHERE Page ='" &Session("Banner") &"'">

</asp:SqlDataSource>

Do I need to 'call' the variable in a parameter or something?

Many thanks, Chris


You can declare the session variable anywhere in your code. Just be sure to do it before you use it. Also, you can use the SessionParameter with the SqlDataSource in conjunction with the SelectCommand.

<asp:SqlDataSource ID="BanRot" runat="server" ConnectionString="<%$ ConnectionStrings:w2gConnectionString %>" SelectCommand="SELECT [Pic], [Nav], [Page], [Alt] FROM [ads-home] WHERE Page =?">
<SelectParameters>
<asp:SessionParameter Name="Page"SessionField="Banner" />
</SelectParameters>
</asp:SqlDataSource>

HTH,
Ryan


Ok, do I need to call it in the script on the same page as the sqldatasource? Or does the Session parameter automatically call it from the memory?

Also, On the initial page where I create the variable, I have

<scriptrunat="server">

Session("banner") = BSAFD

</script>

But I'm getting a 'Declaration Expected' Error, Do I need to put this in a function?

Cheers again, Chris


All you need is the SessionParameter for the SelectCommand and .NET will take care of the rest for you.

When you decalre your session variable, unless BSAFD is a variable, you'll need to enclose it in quotes as it is a string.

Session("Banner") = "BSAFD"

HTH,
Ryan


Ah, brilliant, I now have it working, I got confused with the ? in the WHERE clause, I've now changed it to @.Page - is this just a shortcut meaning put your Parameter here.

Another quick question, I have many pages that have a different variable so it will have a different session variable. If I create a new variable in a different page, will it overwrite the old one or do I need to put some code in there to do it, eg.

Page one:

<% Session("banner") ="value a" %>

I then use that however , but then navigate to page two where I want value b as the session variable, if I put in this:

<% Session("banner") ="value 2" %>

Will it overwrite the old one?

Cheers again, perfect help!! Chris


TheCrawsh wrote:

I've now changed it to @.Page - is this just a shortcut meaning put your Parameter here.

Yes. It's a placeholder for the value.

TheCrawsh wrote:

If I create a new variable in a different page, will it overwrite the old one or do I need to put some code in there to do it, eg.

Yes. Doing so would assign a new value to the session variable.

HTH,
Ryan