[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET VB Session State

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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