0
I was doing a project but there was a problem involving Oledbcommand occurring in two different places:
dr_alu = _dataCommand.ExecuteReader();
if (dr_alu.HasRows == true)
dr_reg_notas = _dataCommand.ExecuteReader();
if (dr_reg_notas.HasRows == true)
I have tried almost everything but I can’t find a way to fix it. I would be very grateful for some help. Thank you.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ProjetoEscola
{
public partial class dgv_notas : Form
{
OleDbConnection conn = Conexao.obterConexao();
OleDbConnection dr_alu;
BindingSource bs_alu = new BindingSource();
OleDbConnection dr_disc;
BindingSource bs_disc = new BindingSource();
OleDbConnection dr_menc;
BindingSource bs_menc = new BindingSource();
OleDbConnection dr_reg_notas;
BindingSource bs_reg_notas = new BindingSource();
String _query;
public dgv_notas()
{
InitializeComponent();
}
private void carregar_aluno()
{
_query = "SELECT * from alunos order by nome";
OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
dr_alu = _dataCommand.ExecuteReader();
if (dr_alu.HasRows == true)
{
bs_alu.DataSource = dr_alu;
cmb_aluno.DataSource = bs_alu;
cmb_aluno.DisplayMember = "nome";
cmb_aluno.ValueMember = "matricula";
lbl_matricula.Text = cmb_aluno.SelectedValue.ToString();
}
else
{
MessageBox.Show("Não temos Alunos Cadastrados !!!!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void carregar_grid()
{
_query = "SELECT Alunos.Nome, Disciplinas.sigla, Disciplinas.descricao, Registro_Mencoes.mencao FROM Disciplinas INNER JOIN (Alunos INNER JOIN Registro_Mencoes ON Alunos.Matricula = Registro_Mencoes.matricula) ON Disciplinas.cod_disciplina = Registro_Mencoes.cod_disciplina order by Alunos.Nome";
OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
dr_reg_notas = _dataCommand.ExecuteReader();
if (dr_reg_notas.HasRows == true)
{
bs_reg_notas.DataSource = dr_reg_notas;
dgv_notas.DataSource = bs_reg_notas;
}
else
{
MessageBox.Show("Não temos Menções Lançadas !!!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void resigtro_Load(object sender, EventArgs e)
{
carregar_grid();
}
}
}
I don’t know why but Has.Rows is also marked
– Lucas
What is the error message
– Bruno H.
Cannot implicitly Convert type 'System.Data.Oledb.Oledbdatareader' to 'System.Data.Oledb.Oledbconnection'
– Lucas
Well that’s guys You’ve tried to state so
– Bruno H.
Oledbdatareader Test = _dataCommand.Executereader();
– Bruno H.
good Voce is using an Oledbconnection = Oledbdatareader
– Bruno H.
Wow, I can’t believe that was it! Thank you very much friend helped me too much... Nor did it cross my mind that
– Lucas
Nothing to do with the question, but to improve the code, check on compare boolean values at link: https://stackoverflow.com/questions/13614839/using-or-equals-for-bool-comparison. In short, this 'if' (dr_reg_notes.Hasrows == true)' is slower than that 'if (dr_reg_notes.Hasrows)'
– Lucas