Parameter Crossing in ASP.NET

Asked

Viewed 51 times

0

I have the following code:

Admin Page:

private void Salvar()
{
    Session.Add("Salvar", txtSobre.Text);
    try
    {
        if (txtSobre.Text != string.Empty)
        {
            Session["Salvar"] = txtSobre.Text.Trim();
        }
    }
    catch(Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

protected void btnSobre_Click(object sender, EventArgs e)
{
    Salvar();
}

User page:

private void Atualizar()
{
    try
    {
        lblSobre.Text = Session["Salvar"].ToString();
    }

    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

protected void btnAtualizar_Click(object sender, EventArgs e)
{
    Atualizar();
}

And I can’t seem to get a text out of TextBox to the label of the other page. Gives the following error:

Undefined object reference for an object instance.

  • First check if the directives, if I’m not mistaken the correct directive is System.Web.UI. Also try to debug, place a breakpoint exactly on the line that is receiving the Session value, position the mouse above the Session and check that the value it is receiving is the desired one.

  • It is important that you treat this data, mainly because it comes by session. In what posted in the question there is no problem, you have to evaluate if the session is being lost/cleaned in your application for some reason while loading the user page.

  • @Maisaberlofa See my answer and confirm that this was your problem.

1 answer

1


The error happens because you are trying to access a session key that has not been declared... to avoid the error you can simply check if it exists before making the assignment.

private void Atualizar()
{
    try
    {
        lblSobre.Text = Session["Salvar"]?.ToString();
    }

    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

protected void btnAtualizar_Click(object sender, EventArgs e)
{
    Atualizar();
}

With the above change adding to ? in Session["Salvar"]?.ToString(); you will no longer receive the error message, however you will not display any value in Textbox until you are assigned some value for that variable in the session.

And here, it seems to me that you are confusing some things... if the access to the administrator and the user’s page is done with different users, instances or locations, both do not share the same session.

It won’t do you any good to add the key "Salvar" in the admin session and expect it to exist in another user’s access. If you want this variable to be available to everyone you use a Application, however this will present the same value for all users of your site and will be reset every time the pool is recycled or your application is restarted.

Admin Page

private void Salvar()
{
    try
    {
        if (txtSobre.Text != string.Empty)
        {
            Application["Salvar"] = txtSobre.Text.Trim();
        }
    }
    catch(Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

protected void btnSobre_Click(object sender, EventArgs e)
{
    Salvar();
}

User page

private void Atualizar()
{
    try
    {
        lblSobre.Text = Application["Salvar"]?.ToString();
    }

    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

protected void btnAtualizar_Click(object sender, EventArgs e)
{
    Atualizar();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.