4
I have the method ChamadaEfetuada(MAluno) : bool
checking whether the call was made to a particular student, this method is used in another method ChamadaEfetuada() : bool
that instead of checking if the call was made to a single student, he checks all students at once, and he only returns true
if the call was made to all students.
But I’m getting an error in my method ChamadaEfetuada() : bool
with the following message:
Undefined object reference for an object instance.
Error occurs on this line:
alunos[i].IdAluno = Convert.ToInt32(registro["id_aluno"]);
Compiler says the problem is in my array alunos
.
Complete code of the method ChamadaEfetuada(MAluno) : bool
:
public bool ChamadaEfetuada(MAluno aluno)
{
string query = "SELECT id_aluno FROM Lista_presenca WHERE id_aluno = " + aluno.IdAluno + " AND data = '" + this.Hoje() + "'";
DadosConexao dados_conexao = new DadosConexao();
SQLiteConnection conexao = this.conexao.Conexao;
conexao.Open();
SQLiteCommand command = conexao.CreateCommand();
command.CommandText = query;
SQLiteDataReader registro = command.ExecuteReader();
bool existe_registro = registro.HasRows;
conexao.Close();
return existe_registro;
}
Complete code of the method ChamadaEfetuada() : bool
:
public bool ChamadaEfetuada()
{
string query = "SELECT id_aluno FROM Alunos";
int total_alunos = 0, alunos_chamada_efetuada = 0;
DadosConexao dados_conexao = new DadosConexao();
SQLiteConnection conexao = this.conexao.Conexao;
conexao.Open();
SQLiteCommand command = conexao.CreateCommand();
command.CommandText = query;
SQLiteDataReader registro = command.ExecuteReader(); //Obtem os IDs de todos os alunos
MAluno[] alunos = new MAluno[registro.FieldCount];
total_alunos = registro.FieldCount;
int i = 0;
while (registro.Read())
{
alunos[i].IdAluno = Convert.ToInt32(registro["id_aluno"]); //Popula o atributo IdAluno do objeto alunos com os IDs obtidos pela query. Porem aqui acontece o erro.
i++;
}
conexao.Close();
for (i = 0; i < registro.FieldCount; i++)
{
if (this.ChamadaEfetuada(alunos[i]))
{
alunos_chamada_efetuada++;
}
}
if (alunos_chamada_efetuada == total_alunos)
return true;
else
return false;
}
The two methods above are part of my class BLLListaPresenca
.
You are sure the number of columns is not what I need, I want to get the number of rows that the query returns me. But I’m having a hard time implementing this business rule, which is to see if the call of the day has been made. You have some hint of how I can restructure this code?
– gato
Start by eliminating all variables that are used zero times, then the ones that are used only once (it is rare for a variable that is used once to be useful, none in this case). Give the class a read
SqlDataReader
. See how to use it, what methods and existing properties they are. Start usingusing
, Use the parameters ofSQLiteCommand
. Switch to `List<T>. There’s more. This is just the beginning.– Maniero
I will take a look here about the parameters of Sqlitecommand and also the class Sqldatareader. The
List<T>
what would it be, an object list like array? I want to go deeper into it.– gato
Ask specific questions with every question you have.
– Maniero