Assign value to variable of type Hiddenfield is giving error

Asked

Viewed 158 times

-1

I have this statement:

HiddenField vhdfCdTipoUsuario = null;

I have this assignment:

vhdfCdTipoUsuario = (HiddenField)e.Item.FindControl("hdfCdTipoUsuario");

If I do this, it makes that mistake:

if ((vhdfCdTipoUsuario.Value != Session["DsTipoUsuario"]) && (Session["DsTipoUsuario"] != null))
                    vhdfCdTipoUsuario.Value = Session["DsTipoUsuario"].ToString();

Object reference not set to an instance of an object.

How do I fix it? The mistake is here:

vhdfCdTipoUsuario.Value = Session["DsTipoUsuario"].ToString();

Session is ok(populated). How I work with type variables HiddenField? How do I assign value to this type of variable? I’ve already removed Tostring() from Session and it still gives tilt.

Here is the full code:

protected void rptGerenciaProcessos_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //Declarações
            HiddenField vhdfCdTipoUsuario = null;
            LinkButton vlkbPriorizar = null;
            string teste = "";

            try
            {
                //Instâncias e Inicializalções
                vhdfCdTipoUsuario = (HiddenField)e.Item.FindControl("hdfCdTipoUsuario");
                vlkbPriorizar = (LinkButton)e.Item.FindControl("lkbPriorizar");

                teste = Session["DsTipoUsuario"].ToString();

                if ((vhdfCdTipoUsuario != Session["DsTipoUsuario"]) && (Session["DsTipoUsuario"] != null))
                    vhdfCdTipoUsuario.Value = teste;

                //Desenvolvimento
                if (vhdfCdTipoUsuario != null)
                    if (int.Parse(vhdfCdTipoUsuario.Value) != 2 )
                        if (vlkbPriorizar != null)
                            vlkbPriorizar.Enabled = false;


            }
            catch (Exception Ex)
            {
                Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
                Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            }
        }
  • Have you tried to check if the assignment line actually assigns anything?

  • Yes, I want to exactly change that line, because it always brings an unexpected result. As there was a change in the scope, it is necessary to change this line: vhdfCdTipoUsuario = (Hiddenfield)e.item.Findcontrol("hdfCdTipoUsuario");

  • But you assign before the if?

  • No, if is after assignment.

  • 1

    Dude, your code looks okay. Plug it in, 'cause it could be a scope problem. Also, through a break point, see which of the objects is triggering the error, if the vhdfCdTipoUsuario or Session["DsTipoUsuario"].

  • Stack the Exception so it’s easier to help you.

  • The error is in vhdfCdTipoUsuario, I’ve seen it. I’ll make an edit and post Too the code.

  • That’s the Stack Trace I took. at Scania.SOMC.Web.frmGESStatusPriorizar.rptGerenciaProcessos_ItemDataBound(Object sender, RepeaterItemEventArgs e) in C:\Repositorio\ICSModuloCredito\Branches\ModuloCredito\ICS_MC\Scania.SOMC.Web\frmGESStatusPriorizar.aspx.cs:line 186

  • Out of nowhere the mistake came back. That’s bone.

Show 4 more comments

1 answer

0

I started running and suddenly no longer gave the exception. It remains the same thing, as in the first post:

if ((vhdfCdTipoUsuario != Session["DsTipoUsuario"]) && (Session["DsTipoUsuario"] != null))
                    vhdfCdTipoUsuario.Value = Session["DsTipoUsuario"].ToString(); 

Browser other questions tagged

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