Array wont save in session variable

Viewed 42

Trying to make a hangman game and I am nearly there. When I type in to the txtBokstavGjett the array won't save when I hit the button. Even though I have a session variable for losning[].

I dont really know what to do from here so any help is much appreciated. The variable names are in norwegian but that shouldnt matter.

public partial class _Default : System.Web.UI.Page
{
    int antallForsok = 0;
    string forsokteBokstaver;
    char[] losning = new char[8];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["antallForsok"] != null)
        {
            antallForsok = (int)Session["antallForsok"];
        }
        labForsok.Text = Convert.ToString(antallForsok);

        if (Session["forsokteBokstaver"] != null)
        {
            forsokteBokstaver = (string)Session["forsokteBokstaver"];
        }
        if (Session["losning"] != null)
        {
            losning = (char[])Session["losning"];
        }
        if (antallForsok == 0)
        {
            for (int k = 0; k < losning.Length; k++)
            {
                losning[k] = '_';
                labRes.Text += " " + losning[k] + " ";
                labTestBox.Text += "" + losning[k] + " ";
            }
        }
    }
    protected void btnKnapp_Click(object sender, EventArgs e)
    {
        labRes.Text = "";
        labTestBox.Text = "";
        string losningOrd = "appelsin";
        char brukerGjett = Convert.ToChar(txtBokstavGjett.Text);
        char[] bokstaver = losningOrd.ToCharArray();
        losning = new char[bokstaver.Length];
        for (int i = 0; i <bokstaver.Length; i++)
        {
            if (brukerGjett == bokstaver[i])
            {
                losning[i] = bokstaver[i];
            }
            else if (!(brukerGjett == bokstaver[i]))
            {

            }
            forsokteBokstaver = brukerGjett.ToString();
            labRes.Text += "" + losning[i];
            labTestBox.Text += " " + losning[i];

        }
        txtBokstavGjett.Text = "";
        antallForsok++;
        labForsoktBokstav.Text += forsokteBokstaver;
    }   
    protected void Page_Unload (object sender, EventArgs e)
    {
        Session["antallForsok"] = antallForsok;
        Session["forsokteBokstaver"] = forsokteBokstaver;
        Session["losning"] = losning;
    }
}
2 Answers

There shouldn't be any issue restoring the array from the Session variable, but you have a line 'losning = new char [...]' that is wiping it out again. You should just be able to remove that line.

The Page_Unload method is being called after the page has already been sent and rendered in the browser. This is too late in the life cycle to save a value to Session.

Why not simply handle storing these values when are calculated?

protected void btnKnapp_Click(object sender, EventArgs e)
{
    labRes.Text = "";
    labTestBox.Text = "";
    string losningOrd = "appelsin";
    char brukerGjett = Convert.ToChar(txtBokstavGjett.Text);
    char[] bokstaver = losningOrd.ToCharArray();
    losning = new char[bokstaver.Length];
    for (int i = 0; i <bokstaver.Length; i++)
    {
        if (brukerGjett == bokstaver[i])
        {
            losning[i] = bokstaver[i];
        }
        else if (!(brukerGjett == bokstaver[i]))
        {

        }
        forsokteBokstaver = brukerGjett.ToString();
        labRes.Text += "" + losning[i];
        labTestBox.Text += " " + losning[i];

    }
    txtBokstavGjett.Text = "";
    antallForsok++;
    labForsoktBokstav.Text += forsokteBokstaver;

    Session["antallForsok"] = antallForsok;
    Session["forsokteBokstaver"] = forsokteBokstaver;
    Session["losning"] = losning;
}
Related