Solved

ASP.NET VB Session State

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

15 Experts available now in Live!

Get 1:1 Help Now