2

Something strange is happening in my web application. I have a static dictionary that I use to hold a collection of simple objects that hold two variables:

static Dictionary<string, linkButtonObject> linkButtonDictonary = new Dictionary<string, linkButtonObject>();

I have a gridview with linkbuttons, data about each one is associated with its button.UniqueId in the Dictionary:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
     LinkButton btn = (LinkButton)e.Row.FindControl("taskLinkButton");
     linkButtonObject currentRow = new linkButtonObject();
     currentRow.storyNumber = e.Row.Cells[3].Text;
     currentRow.TaskName = e.Row.Cells[5].Text;
     linkButtonDictonary.Add(btn.UniqueID, currentRow);
}

Then when the linkbutton is clicked I pull look up the values in the dictionary using the UniqueId, use them in a SQL query and use the retrieved data to populate a gridview, labels and show a popup:

protected void taskLinkButton_Click(object sender, EventArgs e)
{
    //create linkbutton object from sender
    LinkButton btn = (LinkButton)sender;
    //get a list of data relevant to column
    string[] infoData = getInfoData(linkButtonDictonary[btn.UniqueID].storyNumber, 
        linkButtonDictonary[btn.UniqueID].TaskName);
    //assign content of list to labels and gridview
    productDatabaseLabel.Text = infoData[0];
    storyNumberDatabaseLabel.Text = infoData[1];
    taskDatabaseLabel.Text = infoData[2];
    pointPersonDatabaseLabel.Text = infoData[3];
    SqlDataSource6.SelectParameters["storynumber"].DefaultValue = linkButtonDictonary[btn.UniqueID].storyNumber;
    SqlDataSource6.SelectParameters["tasktitle"].DefaultValue = linkButtonDictonary[btn.UniqueID].TaskName;
    infoGridView.DataBind();
    //show popup
    MPE.Show();
}

This all works great and I can click any of the linkbuttons and they correctly create and populate the popup and show it.

My issue is that if I leave the page idle for a few minuites and then click a linkbutton I get the error:

enter image description here

What am I doing wrong and how do I fix it?

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
David Tunnell
  • 7,252
  • 20
  • 66
  • 124

3 Answers3

5

You will lose the static data every time the AppDomain ends, which it will do from time-to-time, for example when IIS decides to recycle your worker process.

See this from somebody who can explain it much better than me! Lifetime of ASP.NET Static Variable

You need to seek out an alternative data storage pattern. Statics are not the way forward for what you are looking to achieve.

Community
  • 1
  • 1
SimonGoldstone
  • 5,096
  • 3
  • 27
  • 38
1

Can you either attach a debugger to your website, or add some diagnostic code? What's the content of the dictionary when the exception occurs?

My guess is that the button's UniqueID is not constant, that the dictionary still contains your data, but the IDs no longer match. Alternatively, maybe the process/appdomain is recycled, and the dictionary is somehow emptied.

Rob
  • 4,327
  • 6
  • 29
  • 55
0

This also happens to me. If you are looking only for a temporary storage storage. Create your own KeyValuePair class then put it in a List<> or in an object[ ].

 class Tupol
    {
        public Tupol() { }
        public Tupol(string key,string value) { }
        public string Key { get; set; }
        public string Value { get; set; }
    }

Initialize:

List<Tupol> temp = new List<Tupol>();

Get value:

foreach(Tupol in temp)
{ if(temp.Key == "foo") { Debug.WriteLine(temp.Value); break; } }

In this instance i choose to be both my "Key" and "Value" pairs as string. (This works for me.)

ikibiki
  • 136
  • 2
  • 10