Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 5th, 2007, 05:28 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Global variable issues.

Hi all,

I wrote code like this

public partial class frmTaskView : System.Web.UI.Page
{
    private int intVar;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          // Call the dataBase sp and assign the variable intVar
          intVar = "10"; // Set through the Databace call
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
       // do search based on the Variable intVar
    }
}


Problem:
        when I click the Search button I am always getting value 0.
If I remove the IsPostBack condition I am getting actual value.This is increasing the Database call.

How I persist the value without the Database call. Please help me.



  #2 (permalink)  
Old July 9th, 2007, 04:29 AM
Authorized User
 
Join Date: Dec 2004
Location: Swindon, Wilts, United Kingdom.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz
Default

The problem with web pages is that global variables are useless, as the page is regenerated on each request.

The best place to store it if you are returning to the same page is in the ViewState, or the Session if you need to persist it across web pages.

As for the reason it work, I am unsure. Normally, the value would stay as 0 as the compiler default it to 0.

Regards,

Dom
  #3 (permalink)  
Old July 9th, 2007, 04:40 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the Replay.

But I have Doute, In view State Can i store objects?


vijil jones



  #4 (permalink)  
Old July 9th, 2007, 07:43 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Anything that can be serialized should be able to be stored in viewstate.

If your object is serializable then you should be able to store it.

-Peter
  #5 (permalink)  
Old July 9th, 2007, 08:22 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear planoie,

 I am very satisfied your quick replay. I achived lot through ur support. Thank you very much.

vijil jones

  #6 (permalink)  
Old July 9th, 2007, 08:26 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 Dear planoie and DZukiewicz

 I am very satisfied your quick replay. I achived lot through ur support. Thank you very much.

vijil jones





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM
Global.asa issues CorumMc Classic ASP Basics 1 November 13th, 2004 12:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.