Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.



Reply With Quote
  #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
Reply With Quote
  #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



Reply With Quote
  #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
Reply With Quote
  #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

Reply With Quote
  #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



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.