Solved

ASP.NET VB Session State

Posted on 2011-09-14
5
281 Views
Last Modified: 2012-06-21
Hi

I Want to save some values in session state, I've been having a play with a really simple example, but cant seem to get it to work.  My code is below

Can any one help

Thanks

Brasso


ASP:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm4.aspx.vb" Inherits="BIS.WebForm4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:Button ID="Button1" runat="server" Text="Button" />

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>



VB:

Public Class WebForm4
    Inherits System.Web.UI.Page

    Private Sub WebForm4_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        TextBox1.Text = CType(Session.Item("Tester"), String)
    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Session("Tester") = "Hello"
    End Sub
End Class

Open in new window

0
Comment
Question by:brasso_42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36535300
In your web.config file check whether session mode is ON. If it is off it wont work. Refer below links.

http://msdn.microsoft.com/en-us/library/ms972429.aspx
http://wiki.asp.net/page.aspx/57/session/
http://www.codeproject.com/KB/aspnet/ExploringSession.aspx
0
 
LVL 1

Author Comment

by:brasso_42
ID: 36535323
Hi

in my webconfig I have

 <sessionState mode="InProc" timeout="480" />

is that enough?

Thanks

Brasso
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36535387
Well. The problem is you are getting value in INIT method. At that time session value wont be set. Because you are setting in Button click event. It will fire after page load.

Get the session at Page Load event in the condition

if(IsPostBack)
{
 ///  Get your session
}
0
 
LVL 1

Author Comment

by:brasso_42
ID: 36535526
Hi

If i put it in page load it takes two clicks to work.  How do I read in the values on the first click?

Thanks for your help!

Brasso
0
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 500 total points
ID: 36547156
To get session value on button click first you need to set session value on page load and on button click you can access that or you can set that value in Page Init event also.

Your VB code will look like this;

Public Class WebForm4
        Inherits System.Web.UI.Page

        Private Sub WebForm4_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        End Sub

        Private Sub WebForm4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set session value
            Session("Tester") = "Hello"
        End Sub


        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'Get session value
            TextBox1.Text = CType(Session.Item("Tester"), String)
        End Sub
    End Class

Articles on ASP.NET Session
http://www.codeproject.com/KB/aspnet/ExploringSession.aspx
http://msdn.microsoft.com/en-us/library/ms972429.aspx

Hope this will work.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question