The idea of the software is to select a name in the combobox (cmb_nomeGu) and thus to play in the listbox (lst_result) all the dates found that contain the chosen name, then click on the existing dates to search the last information, which is the occurrence summary (txt_current). The problem happens when I try to click on a date in the listbox to search for the remaining information in the database from the selected date, to after that fill the text box (txt_ocorrencias) with the information found. The error that appears is: Object reference not set to an instance of an object.
OperacaoBancooperacao=newOperacaoBanco();MySqlDataReaderdados;privatevoidlst_resultado_SelectedIndexChanged(objectsender,EventArgse){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();
}
}
}
FOLLOWTHECLASSESFORBANKCONNECTIONANDOPERATION
CONNECT
classConnection{publicstaticMySqlConnectionconexao=null;privateStringstringconnection="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();
}
}
CLASS OPERATION
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;
}
}