views:

1021

answers:

2

I am using a hashtable to store key-value pairs and I initialize this hashtable (ddl_ht) in the method CreateDropDownLists(). However, when I check the value of "currentItem" in my SelectedIndexChanged method, this value is null. Even though I checked the value of

(string)ddl_ht[key[1]]

in my Watch window and it shows a value (not null). Do you understand why this currentItem is null?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADONET_namespace;

namespace AddFileToSQL
{
    public partial class DataMatch : _Default
    {
        protected System.Web.UI.WebControls.PlaceHolder phTextBoxes;
        protected System.Web.UI.WebControls.PlaceHolder phDropDownLists;
        protected System.Web.UI.WebControls.Button btnAnotherRequest;
        protected System.Web.UI.WebControls.Panel pnlCreateData;
        protected System.Web.UI.WebControls.Literal lTextData;
        protected System.Web.UI.WebControls.Panel pnlDisplayData;

        protected static string inputfile2;
        static string[] headers = null;
        static string[] data = null;
        static string[] data2 = null;
        static DataTable myInputFile = new DataTable("MyInputFile");
        static string[] myUserSelections;
        static Hashtable ddl_ht = new Hashtable();

        // Page Load 
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.NumberOfControls = 0;
            }
        }

        // Add DropDownList Control to Placeholder
        private void CreateDropDownLists()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);
                ddl.ID = "DropDownListID " + (counter + 1).ToString();
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataSource = dr;
                ddl.DataBind();

                //myUserSelections[counter] = "";

                ddl.AutoPostBack = true;
                ddl.EnableViewState = true; //Preserves View State info on Postbacks
                ddl.Style["position"] = "absolute";
                ddl.Style["top"] = 100 * counter + 80 + "px";
                ddl.Style["left"] = 250 + "px";
                ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                ddl_ht.Add(counter, ddl.SelectedValue);

                pnlDisplayData.Controls.Add(ddl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
                pnlDisplayData.Visible = true;
                pnlDisplayData.FindControl(ddl.ID);
               // pnlDropDownList.FindControl(ddl.ID);
                dr.Close();
            }
        }

        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender; 
            string[] value=(ddl.SelectedValue).Split(' ');
            string[] key = ddl.ID.Split(' ');
            string currentItem=(string)ddl_ht[key[1]];
            //if (String.IsNullOrEmpty(currentItem))
            //{
            //    ddl_ht.Add(key[1], value[0]);
            //}
            if (currentItem != ddl.SelectedValue)
            {
                ddl_ht.Remove(key[1]);
                ddl_ht.Add(key[1], ddl.SelectedValue);
            }
        }

        // Add TextBoxes Control to Placeholder
        private void RecreateDropDownLists()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);

                ddl.ID = "DropDownListID " + (counter + 1).ToString();
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataSource = dr;
                ddl.DataBind();
                myUserSelections[counter] = "";
                dr.Close();

                ddl.AutoPostBack = true;
                ddl.EnableViewState = true; //Preserves View State info on Postbacks
                ddl.Style["position"] = "absolute";
                ddl.Style["top"] = 100 * counter + 80 + "px";
                ddl.Style["left"] = 250 + "px";
                ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                pnlDisplayData.Controls.Add(ddl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
            }
        }

        // Create TextBoxes and DropDownList data here on postback.
        protected override void CreateChildControls()
        {
            // create the child controls if the server control does not contains child controls
            this.EnsureChildControls();

            // Creates a new ControlCollection. 
            this.CreateControlCollection();

            // Here we are recreating controls to persist the ViewState on every post back
            if (Page.IsPostBack)
            {
                RecreateDropDownLists();
                RecreateLabels();
            }
            // Create these conrols when asp.net page is created
            else
            {
                PopulateFileInputTable();
                CreateDropDownLists();
                CreateLabels();
            }

            // Prevent child controls from being created again.
            this.ChildControlsCreated = true;
        }

    }
}
+1  A: 

You shouldn't store your HashTable as a static field of your page class, since it will be shared between sessions because the life cycle of a static variables in ASP.NET is within the life of the appdomain, consider storing it in the ViewState:

private Hashtable ddl_ht
{
    get 
    {
       return ViewState["ddl_ht"] as HashTable;
    }
    set
    {
       ViewState["ddl_ht"] = value;
    }
}
CMS
This code should probably be changed into storing the hashtable if it's created.
Simon Svensson
@Simon: You're right, but maybe it's easier just return null if the object is not on ViewState, and provide a setter... check the edit...
CMS
Thank you for the sample code. This solution worked with a couple tweaks! Here is what else I had to do which is pretty basic. In my PageLoad, I initialized ddl_ht and then in the SelectedIndexChanged method I had to convert the key to an integer type. That was why currentItem was null.
salvationishere
+3  A: 

You are going to have all kinds of threading problems with this setup. Your hashtable is static, and every hit on your website is going to create a new instance of your class on a new thread that will try to access the same hashtable - and since each new hit to the page will initially call CreateDropDownLists, your hashtable will be reinitialzed for every new user to the page.

womp