ashkufaraz
ashkufaraz

Reputation: 5297

global variable is null

Dictionary<string, string> propertyCompany = new Dictionary<string, string>();//gloabal variable

protected void Page_Load(object sender, EventArgs e)
{
   if(!isPostBack){
      propertyCompany .add("a","1"); 
      propertyCompany .add("b","2");
      propertyCompany .add("c","3");
   }
}

protected void btnGetProperty_Click(object sender, EventArgs e)
{
      string a=propertyCompany["a"];//error this key is not exist
     //propertyCompany is null
}

when define a propertyCompany and fill in form_load. in on click button propertyCompany is null!?

i use a static but i does not understand sometime say error is null.

Upvotes: 4

Views: 4496

Answers (6)

dlev
dlev

Reputation: 48596

I'm pretty sure your title should be "global variable does not have the data I want." The dictionary will be constructed each time the page is loaded (postback or otherwise), but because of this line:

if(!isPostBack) {
}

it won't have the data you want on a button click.

In order to notify the page of the click, a post back is performed, so saying !isPostBack (which I'm assuming is set somewhere via Page.IsPostBack) is also saying "if I haven't clicked the button", which is of course not what you want.

In order to get the functionality you want, you should either move the population of the dictionary out of that if block, or else have an else condition that also populates it with data you want.

Another alternative to using the class variable is to store the data in another location. Options include ViewState, Session, Application (if it really is application-wide data), the Cache, and some others as well. It's not clear exactly what the dictionary is doing, so it's hard to say which location would be most appropriate.

Upvotes: 1

Donatas K.
Donatas K.

Reputation: 866

try this

    Dictionary<string, string> propertyCompany;//gloabal variable

protected void Page_Load(object sender, EventArgs e)
{ 
   propertyCompany = new Dictionary<string, string>();
   if(!isPostBack){
      propertyCompany .add("a","1"); 
      propertyCompany .add("b","2");
      propertyCompany .add("c","3");
   }
}

protected void btnGetProperty_Click(object sender, EventArgs e) { //propertyCompany is null }

Upvotes: -1

Tim Schmelter
Tim Schmelter

Reputation: 460028

Every variable defined in a class inheriting Web.UI.Page will be destroyed at the end of the Page-Lifecycle, hence it will be null in a Postback if you don't reinitialize it.

One way to persist it across postbacks is to store it in a Session-variable.

You will find a complete list of all options on how to persist variables across postbacks here: http://msdn.microsoft.com/en-us/magazine/cc300437.aspx

  • Application
  • Cookies
  • Form Post / Hidden Form Field
  • QueryString
  • Session
  • New State Containers in ASP.NET
  • Cache
  • Context
  • ViewState
  • Web.config and Machine.config Files Conclusion

It's in the nature of HTTP-protocol that it is stateless.

Upvotes: 1

curtisk
curtisk

Reputation: 20175

How are you accessing the items in propertyCompany in the button click event? If you are doing that incorrectly, that is more likely the issue.

Upvotes: 0

Anders Forsgren
Anders Forsgren

Reputation: 11101

One way to get your dictionary to live between requests is to declare it static, or in viewstate as was suggested earlier.

Upvotes: 0

Ales Ruzicka
Ales Ruzicka

Reputation: 2790

Each request creates new page object, therefore you cannot use in second request (bnt click) dictionary you have created in first request (load without postback)

Remove test for postback for quick fix.

Other fix posibilities: * store dictionary in viewstate.

Upvotes: 4

Related Questions