Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How To Declare and Use Global Variables in your ASP.Net Web Application

Author: DevASP
Download Source Code : 323_session_variable.zip

In ASP.net global variables are known as session variable and these are very easy to declare and use in Visual Studio.Net.

In this simple example you will learn to declare and use of session variables. Firstly create a webform and place a label having text Enter Value, a textbox with name txtvalue. Then create a button having text save and finally create a button with text goto form2.

 

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 344px; POSITION: absolute; TOP: 24px" runat="server" Width="80px" Height="24px" Font-Size="Large">Devasp</asp:Label>

 

<asp:TextBox id="TXTVALUE" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 152px" runat="server" Height="32px" Width="104px"></asp:TextBox>

 

<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 72px; POSITION: absolute; TOP: 200px" runat="server" Height="24px" Width="64px" Text="SAVE"></asp:Button>

 

<asp:Button id="Button2" style="Z-INDEX: 105; LEFT: 72px; POSITION: absolute; TOP: 256px" runat="server" Height="32px" Width="128px" Text="GOTO FORM2"></asp:Button>

  • Now write code on button1 click event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

        Session("VALUE1") = TXTVALUE.Text

 

End Sub

  • Now insert code on button2 click event

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

 

        Server.Transfer("WEBFORM2.ASPX")

 

End Sub

  • Now insert another form and create two labels on that form:

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 56px" runat="server" Width="192px" Height="32px" DESIGNTIMEDRAGDROP="21" Font-Size="Large" BackColor="Silver"> STORED VALUE</asp:Label>

 

<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 56px" runat="server" Width="360px" Height="32px" Font-Size="Large" BackColor="#E0E0E0"></asp:Label>

  • Now write code on form2 load event

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

      Label2.Text = Session("VALUE1")

 

End Sub

  • This statement is for declare and set value of the session variable

Session("VALUE1") = TXTVALUE.Text

  • In this statement value1 is the name of the session variable and you can use this with this way

TXTVALUE.Text =Session("VALUE1")

           

Article Comments
Simple and Helpful

Posted on 1/29/2007 1:36:16 AM by Asif Iqbal

This was really helpful as I was stuck while creating my project.

Posted on 3/7/2007 4:50:36 AM by Siddhartha Srivastava

I used the method mentioned above, but now i am getting the following error.
"Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive "
Can you help me?

Posted on 4/11/2007 7:22:24 AM by pooja

sir,
it seems to be okay, but i would like to know about the scenario where i need to change the value of session variable in every form

Posted on 4/26/2007 5:35:38 AM by kapil bajpai

really good, thanks mate, simple but solved an important problem for me :)

Posted on 5/11/2007 11:58:48 AM by dave

Sir,

This is quite useful, but what about global function or sub which is to be used in all the forms.

Please advice

Posted on 5/30/2007 9:52:53 AM by SF

session variable will expire once ur browser is closed (i,e,. it is not available for other user) so it cannot be use by the other member using the application

Posted on 12/31/2007 11:42:36 AM by Arman Syed

Thanks

Posted on 1/19/2008 2:04:17 AM by great

Thanks

Posted on 1/19/2008 2:05:49 AM by great

This was very very helpful; however, is there any way to pass or access the session variable in your asp.net source code? For example if I used this code to collect and make a session variable from the login webcontrol's password textbox and then wanted to use that value in my asp.net code?

Posted on 2/6/2008 7:23:55 PM by jason

Great!
It was really helpfull for me.

Posted on 4/10/2008 4:50:11 AM by Ventuks

Good.
It was really helpfull for me.

Posted on 4/30/2008 8:29:06 AM by guruvu setty

Very helpfull for me too! I was trying for days with static variables and this solution was great!

Thanks

Posted on 9/10/2008 1:58:26 PM by Try

this is very helpful and easy to understand.

Posted on 9/17/2008 4:12:59 AM by Vaibhav

How To Declare and Use Global Variables in your VB.Net 2008

Posted on 9/23/2008 1:51:51 AM by priyank

How To Declare and Use Global Variables in your VB.Net 2008 not in asp

Posted on 9/30/2008 12:44:47 AM by priyank gupta

I have over 45 global variables and I want to declare them in Page_Load so that I can use in sub_routines. How can I do this?

Posted on 10/1/2008 4:21:05 PM by JRK

This was extremely amazing.
--------------------


--------------------

Posted on 10/27/2008 6:54:19 AM by Imran

Please can anybody help me regarding the Session problem while i am using in my login page. I am storing my username in session as Session["username"] but i have always session problem while i am using it.Its not working.
I am not able to cross login page........

Posted on 11/1/2008 7:32:40 AM by Amit Habu

Please send me how to declare globally the connection string. and how to access it in ado .net.

Posted on 11/15/2009 2:51:37 PM by Krushna Prasad Mohanty

Hi Krushna Prasad Mohanty,

You simply can define connection string as global variable same as other global variable

public string Constr = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword";

Now simply use it in you code

using (SqlConnection connection = new SqlConnection(Constr))
{
connection.Open();
// Now you can access database here
}


Thanks.

Posted on 11/16/2009 3:55:12 AM by zunnair

hi.
i am from asia.
currently I'm doing this in visual studio 2005 Professional Edition.
but I'm getting 2 errors that says : 'System.web.ui.page.session' is a 'property' but is being used like a 'method' .
the first error in button2_click event and second in form2 page_load.
I've also tried removing the .tostring just for kicks but still didnt fix it.
please help me.


Posted on 12/30/2009 2:14:20 AM by alex

That was good, I will equally like to know how to make a sub public to all my aspx page

Posted on 3/1/2010 5:40:59 AM by CHUKS EKE

session is closed when i create pdf format of crystal report.
crystal report i use push method. the session of the previous page is used in query to generate the report.
customerReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ReportMain")

Posted on 6/3/2010 8:41:51 AM by Anirudhya Sanyal

It's very simple and good, but, i, want know that, can, i, use same session variable other page? or i, have to declare different session varibales?
Kindly advice me...
Thanks in advance

Posted on 6/10/2010 6:13:56 AM by Gangadhar Nalla

thanks easy and very intelligent

Posted on 8/27/2010 12:02:10 PM by ary

vry good man..thanks for this.

Posted on 10/6/2010 4:37:57 AM by nachiket

Thank you. The sample logic provided has been really helpful to me.

Regards,
Sunil

Posted on 11/27/2010 2:05:44 PM by sunil

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with System.IO.Path Class

Disclaimer - Privacy
© 2002-2014 DevASP.net