What I am attempting to do is preserve a session object that stores a Dictionary .

I need this dictionary in order to keep a running list that matches up to an asp:Gridview.

Every time the page loads, I'm checking the Dictionary and highlighting any matches in the Gridview.

However, every time Page_Load occurs, the Session["Rolls"] is coming up as null. Now, I'm also highlighting matching entries in a buttonClick event and the Dictionary is preserved up until I initiate a different event (like another button click OR GridView_RowEditing/GridView_RowUpdating). Are there any principals of Session variables that I'm not paying attention to?

Google Chrome console also recognizes the Session throughout every action, but in Debug, every Page_Load brings up a null Session["Rolls"].

Here's my Page_Load code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Rolls"] = new Dictionary<string, int>();
        }
        else
        {
            WoSource.SelectCommand =
            "SELECT WorkOrderNo, RollNumber, ModelNumber, QtyGood, QtyRun FROM RFID_Inventory WHERE WorkOrderNo= '" + woText.Text + "'";

            var currentDict = (Dictionary<string, int>) Session["Rolls"];
            if (currentDict == null)
            {

            }
            else
            {
                foreach (var entry in currentDict)
                {
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        var dataKey = GridView1.DataKeys[row.RowIndex];
                        if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 0))
                        {
                            row.BackColor = System.Drawing.Color.Red;
                            break;
                        }
                        if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 1))
                        {
                            row.BackColor = System.Drawing.Color.Green;
                            break;
                        }
                    }
                }   
            }
        }
    }

EDIT: Discovered that the RowEditing/RowUpdating events are not preserving the GridView Backcolor highlighting that I'm doing.

Is there something I can add to those event calls?

Here is my RowEditing event:

protected void GridView1_RowEditing(object sender, EventArgs e)
    {
        var currentDict = (Dictionary<string, int>)Session["Rolls"];
        if (currentDict == null)
        {

        }
        else
        {
            foreach (var entry in currentDict)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    var dataKey = GridView1.DataKeys[row.RowIndex];
                    if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 0))
                    {
                        row.BackColor = System.Drawing.Color.Red;
                        break;
                    }
                    if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 1))
                    {
                        row.BackColor = System.Drawing.Color.Green;
                        break;
                    }
                }
            }
        }
    }

EDIT2: My issue was solved. My session wasn't coming back null. I actually needed to add my GridView highlighting method to the GridView_RowDataBound event. Turns out I was just trying to highlight the gridview at the wrong time. I marked the Kishore's answer as correct so that users can see my reply. Thanks for everybody's help.

Related posts

Recent Viewed