I cannot select a listbox item and get the other information in the Database from that selected item

Asked

Viewed 222 times

0

The idea of the software is to select a name in the combobox (cmb_nameGu) and with this play in the listbox (lst_result) all found dates that contain the chosen name, to later click on existing dates to search for the last information, which is the summary of the occurrence (txt_occurrence). The problem happens when I try to click on a date in the listbox to search for the other information in the database from the selected date, and after that fill the text box (txt_occurrences) with the information found. The error that appears is: Undefined object reference for an instance of an object.

Form Pesquisar

OperacaoBanco operacao = new OperacaoBanco();
MySqlDataReader dados;



private void lst_resultado_SelectedIndexChanged(object sender, EventArgs e)
{
    if(cmb_nomeGu.Text != "")
    {        
        try
           {
           string dataServico = "", ocorrenciaObtida = "";
           dataServico = lst_resultado.SelectedItem.ToString();
           //MessageBox.Show(dataServico);
           dados = operacao.Select("SELECT ds_ocorrencia FROM tb_livro2018 WHERE dt_servico = '" + dataServico + "'");

           grp_resumo.Visible = true;

           while (dados.Read())
              {                  
              ocorrenciaObtida = Convert.ToString(dados[0]);
              txt_ocorrencias.Text = ocorrenciaObtida;
              }
           }
        catch (Exception erro)
           {
           MessageBox.Show("Não foi possível realizar a pesquisa \n Erro:" + erro);
           }
        finally
           {
           Connection.fecharConexao();
           }
        }



}

erro

BELOW THE CLASSES FOR CONNECTION AND OPERATION IN THE BANK

CONNECTION

class Connection
{
    public static MySqlConnection conexao = null;
    private String stringconnection = "server=**********;User Id = ********; password=********;database=*************";

    public void tentarAbrirConexaoLocal()
    {
        conexao = new MySqlConnection();
        conexao.ConnectionString = stringconnection;
        conexao.Open();
    }

    public Connection()
    {
        try
        {
            tentarAbrirConexaoLocal();
        }
        catch
        {
            Console.WriteLine("Não foi possível validar seu acesso.Tente novamente.");
        }
     }
    public static MySqlConnection getConexao()
    {
        new Connection();
        return conexao;
    }
    public static void fecharConexao()
    {
        conexao.Close();
    }
}   

OPERATION CLASS

class OperacaoBanco
{
    private MySqlCommand TemplateMethod(String query)
    {
        MySqlConnection Conexao = Connection.getConexao();
        MySqlCommand Comando = new MySqlCommand(query, Conexao);

        try
        {
            Comando.ExecuteNonQuery();
            return Comando;
        }
        catch
        {
            return Comando;
        }
    }

    public MySqlDataReader Select(String query)
    {
        MySqlDataReader dadosObtidos = TemplateMethod(query).ExecuteReader();
        return dadosObtidos;
    }

    public Boolean Insert(String query)
    {
        MySqlConnection Conexao = Connection.getConexao();
        MySqlCommand Comando = new MySqlCommand(query, Conexao);

        try
        {
            Comando.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public Boolean Update(String query)
    {
        MySqlConnection Conexao = Connection.getConexao();
        MySqlCommand Comando = new MySqlCommand(query, Conexao);

        try
        {
            Comando.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
    }
    public Boolean Delete(String query)
    {
        MySqlConnection Conexao = Connection.getConexao();
        MySqlCommand Comando = new MySqlCommand(query, Conexao);

        try
        {
            Comando.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
    }
  • 1

    NullReferenceException means that you are using an object that is null. What line of code this error occurs on ?

  • According to the exception returned, the error is on line 222. Is another thing treating the date correctly? I noticed that you are using Mysql, and dates are treated in this format AAAA/MM/DD.

  • I changed them, put everything in string and format dd/MM/yyyy

  • line 222 refers to the code: 'dataServico = lst_result.SelectedItem.Tostring();'

  • Selectedit Exchange for Selectedvalue or Selectedtext

  • I tested with Selecteditem and Selectedvalue... with both the error to, but only search for the first information (txt_occurrence) of the first item in the list, even if I click on the others. In summary, the 'Summary' field is only filled with the 1st Index, regardless of the selected item.

  • Looking better here.... it seems that all the dates listed in the listbox are as a single Indice...

  • @I can’t really understand what you want to do. Your original question describes that you need to select data in the database with a listbox item. Reading your comments seems that there is another problem that is not at all clear (database error/logic of the language/form/other factor). May be more specific ?

Show 3 more comments
No answers

Browser other questions tagged

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