Exception of type System.Nullreferenceexception

Asked

Viewed 579 times

-2

Soon after login in the system I try to access a page and the system sends me again to the login page and soon after the login again held happens this exception: "An Exception of type 'System.Nullreferenceexception' occurred in Meuprojeto.dll but was not handled in user code".

Follow the line where the error occurs:

int cod_centro_custo = (int)Session["cod_centrodecusto_usuario"];

Doing some research apparently the code is losing my variables stored in Session for no reason.

The project I’m working on is a Webforms with several projects. Some of these projects are used to get information from an SQL Server database while others only serve as objects in which I store information that was obtained with the queries. On the Default.aspx page I do this code to validate the login and assign values to some Sessions:

            String login = login_username.Value;
        String senha = login_pass.Value;

        usuario_valeTO cls_usuario = usuario_valeBLL.Getusuario_valeByLoginAndSenha(login, senha);


        if (cls_usuario.cod_usuario >= 1)
        {
            DetalhesTO detalhes = DetalhesBLL.GetDetalhesByCliente(cls_usuario.cod_cliente_usuario);

            Session.Add("detalhes", detalhes);
            Session.Add("cod_centrodecusto_usuario", cls_usuario.cod_centrodecusto_usuario);
            Session.Add("cod_cliente_usuario", cls_usuario.cod_cliente_usuario);
            Session.Add("cod_usuario", cls_usuario.cod_usuario);
            Session.Add("nivel", cls_usuario.nivel);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, login_username.Value, DateTime.Now, DateTime.Now.AddMinutes(2880), RememberMeSet.Checked, cls_usuario.cod_cliente_usuario.ToString() + "//" + cls_usuario.cod_centrodecusto_usuario.ToString(), FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

            if (ticket.IsPersistent)
            { cookie.Expires = ticket.Expiration; }
            Response.Cookies.Add(cookie);
            Response.Redirect(FormsAuthentication.GetRedirectUrl(login_username.Value, RememberMeSet.Checked));
        }
        else
        {
            div_erro.Visible = true;
        }

By debugging I see the Sessions being filled, but then everything gets lost. Any idea why the Session was lost?

[EDIT]

Well, I managed to solve the problem, you probably didn’t understand why I did solve the problem, but it solved...

The problem apparently was in the fact that I had set my worspace on a network location, one of these areas on a server that you map and can use as a storage disk. I remembered that I had worked on a project that couldn’t run the project locally if its files were in one of these network areas, so I decided to remove all the files from my project from this area and put them in my desktop folder and the problem was solved.

If someone can give an explanation as to why this change made the local execution of the project work it would be interesting to put the reason here. So I thank those who tried to help.

2 answers

2


You can test whether the "Session" is not null before passing the value.
Example:

int cod_centro_custo = 0
if (Session["cod_centrodecusto_usuario"] != null)
   cod_centro_custo = (int)Session["cod_centrodecusto_usuario"];

This will avoid the error of NullReferenceException but I don’t know why the system loops.

1

You can do it this way too:

int cod_centro_custo = Session["cod_centrodecusto_usuario"] != null ?(int)Session["cod_centrodecusto_usuario"]:0;

Browser other questions tagged

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