Solved

ASP.NET VB Session State

Posted on 2011-09-14
5
275 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now