?
Solved

ASP.NET VB Session State

Posted on 2011-09-14
5
Medium Priority
?
285 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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