Soon after logging in to the system I try to access a page and the system sends me back to the login page and immediately after login again, this exception happens: "An exception of type 'System.NullReferenceException' occurred in MyProject. dll but was not handled in user code ".
The line where the error occurs follows:
int cod_centro_custo = (int)Session["cod_centrodecusto_usuario"];
Doing some research apparently the code is losing my stored variables in the Session for no reason at all.
The project I'm working on is a WebForms with multiple projects. Some of these projects are used to get information from a SQL Server database while others only serve as objects in which I store information that was obtained with the queries. In the Default.aspx page I make 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;
}
Debugging I see Sessions being filled, but then everything is lost. Any ideas on the reason for the loss of the Session?
[EDIT]
Well, I managed to solve the problem, you probably did not understand why I solved the problem, but solved ...
The problem apparently was in the fact that I had set up my worspace in 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 could not run the project locally if its files were in one of these network areas, so I decided to remove all the files in my project from this area and put them in my desktop folder and the problem has been resolved.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 thank you to those who have tried to help.