Persistent error in C # database

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 not 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();
        }

    }
}
    
asked by anonymous 11.08.2017 / 02:15

2 answers

3

You are using object OleDbConnection to receive a OleDbDataReader object, so the error

  

Can not implicitly convert type 'System.Data.OleDb.OleDbDataReader' to   'System.Data.OleDb.OleDbConnection'

 OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
 OleDbDataReader varavelReader = _dataCommand.ExecuteReader();
 if (varavelReader.HasRows == true)
{
   ....
}

As can be seen in the example below taken from the MSDN .

public void CreateReader(string connectionString, string queryString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[0].ToString());
        }
        reader.Close();
    }
}
    
11.08.2017 / 02:34
1
The problem is that you have declared the variables dr_reg_notas and dr_alu as variables OleDbConnection and are trying to use them as OleDbDataReader variables.

The right thing to do would be to create another variable:

OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
var leitor = _dataCommand.ExecuteReader();
if (leitor.HasRows == true)
{
}
    
11.08.2017 / 02:30