Undefined object reference for an instance of an Asp.net object

Asked

Viewed 274 times

0

blz? Next, I have a problem with the exception "Object reference not defined for an instance of an object", I have already sought several solutions, however, none satisfies and/or corrects my problem. In case anyone can help me...

string usuario;
protected void Page_Load(object sender, EventArgs e) {

  //Área referente a consulta na base de dados em relação as metas da operadora

  SqlConnection conexao = new SqlConnection(@ "Data Source=.\SQLEXPRESS;Initial Catalog=DB_COOPERATIVE;Integrated Security=True;"); //Definição da string de conexão
  conexao.Open(); //É aberto a conexão com a base de dados

  //Recuperar o código da pessoa através de seu nome
  SqlCommand comandos2 = new SqlCommand("SELECT CODIGO_FUNCIONARIO from USUARIO where LOGIN_USUARIO='" + usuario + "'");
  comandos2.Connection = conexao;
  int cod_funcionario = Convert.ToInt16(comandos2.ExecuteScalar());

  //Por fim, pelo código do funcionário obtido, é iniciado a busca por suas metas
  SqlCommand comandos4 = new SqlCommand("SELECT VALOR_META from METAS where CODIGO_FUNCIONARIO='" + cod_funcionario + "'");
  comandos4.Connection = conexao;
  int metaArrecadacao = Convert.ToInt16(comandos4.ExecuteScalar());
  double valorSemanal = metaArrecadacao / 5;
  double valorDiario = metaArrecadacao / 30;

  //Por fim é exibido ao operador as suas respectivas metas
  //lblmensal.Text = Convert.ToString(metaArrecadacao);
  //lblsemanal.Text = Convert.ToString(valorSemanal);
  //lbldiaria.Text = Convert.ToString(valorDiario);

  //Eventos ao momento em que o form é carregado
}

protected void Page_InitComplete(object sender, EventArgs e) {
  string usuario = String.Format(Session["usuario"].ToString());
  lblNome.Text = usuario;(AQUI OCORRE O ERRO)


}

The error happens in the line I referenced. When I run initially, the error does not occur, but when I change page in my project, the error happens.

  • 1

    I would need to see other parts of the code, but clearly there is no field lblNome. Unless the error is somewhere else. There are other problems in the code.

  • Opa, speaks Maniero blz? Next, this error appears in all areas related to manipulation with the page Abels

  • Abels are with the attribute runat="server"?

  • Hello, this error occurs when I leave the homepage and go to another page within the application. If I am on the home page, this error is not triggered

2 answers

0

I believe you’re using it mistakenly Page_InitComplete

See what it says in the documentation:

The event Initcomplete is called at the end of the page startup stage. At this stage of the page lifecycle, all declared controls on the page are initialized, but the status of the page is not yet completed. You can access the server controls, but they will still not contain user returned information.

Excerpt taken from this site:

https://msdn.microsoft.com/pt-br/library/system.web.ui.page.initcomplete(v=vs.110). aspx

That old advice: put a break point and check when you access this method.

  • Speaks SM_S , blz? So, I tried to insert this excerpt in Page_load, however, also occurs the same error...

  • that one label exists?

  • If you can, post the part of . aspx that the input is declared in HTML, and your .designer.aspx

  • <Asp:Label ID="lblNome" runat="server" Text=""></Asp:Label>

  • At first as I had commented it works without major problems, however, when I access another page of my application, the same does not work.

0


Solved personal!!! It seems silly, but it makes a lot of sense, it was not treating the relation of null values, so the exception was fired. To correct I made the following implementation:

namespace Projeto_Coopera
{
    public partial class dashboardOp : System.Web.UI.Page
    {
        string usuario = "";

        protected void Page_Load(object sender, EventArgs e)
        {
                    usuario = String.Format(Session["usuario"].ToString());

                    if (lblNome != null)
                    {
                        lblNome.Text = usuario;
                    }

                    //Área referente a consulta na base de dados em relação as metas da operadora
                    SqlConnection conexao = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=DB_COOPERATIVE;Integrated Security=True;"); //Definição da string de conexão
                    conexao.Open(); //É aberto a conexão com a base de dados

                    //Recuperar o código da pessoa através de seu nome
                    SqlCommand comandos2 = new SqlCommand("SELECT CODIGO_FUNCIONARIO from USUARIO where LOGIN_USUARIO='" + usuario + "'");
                    comandos2.Connection = conexao;
                    int cod_funcionario = Convert.ToInt16(comandos2.ExecuteScalar());

                    //Por fim, pelo código do funcionário obtido, é iniciado a busca por suas metas
                    SqlCommand comandos4 = new SqlCommand("SELECT VALOR_META from METAS where CODIGO_FUNCIONARIO='" + cod_funcionario + "'");
                    comandos4.Connection = conexao;

                    int metaArrecadacao = Convert.ToInt16(comandos4.ExecuteScalar());
                    double valorSemanal = metaArrecadacao / 5;
                    double valorDiario = metaArrecadacao / 30;

                    //Por fim é exibido ao operador as suas respectivas metas

                    //lblmensal.Text = Convert.ToString(metaArrecadacao);
                    //lblsemanal.Text = Convert.ToString(valorSemanal);
                    //lbldiaria.Text = Convert.ToString(valorDiario);

        }





    }
}

Browser other questions tagged

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