Persistent error in C# database

Asked

Viewed 44 times

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

  • What is the error message

  • Cannot implicitly Convert type 'System.Data.Oledb.Oledbdatareader' to 'System.Data.Oledb.Oledbconnection'

  • Well that’s guys You’ve tried to state so

  • Oledbdatareader Test = _dataCommand.Executereader();

  • good Voce is using an Oledbconnection = Oledbdatareader

  • Wow, I can’t believe that was it! Thank you very much friend helped me too much... Nor did it cross my mind that

  • 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)'

Show 3 more comments

2 answers

3

You are using object OleDbConnection to receive an object OleDbDataReader, therefore the mistake

Cannot 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)
{
   ....
}

How can be Noted in the example below taken from the documentation site 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();
    }
}
  • Thank you very much, that’s right! I hadn’t even thought about it. Thank you!

  • @Matheus The answer solved your question? Do you think you can accept it? See the tour if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1

The problem is that you declared the variables dr_reg_notas and dr_alu as variables OleDbConnection and is trying to use them as variables OleDbDataReader.

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)
{
}

Browser other questions tagged

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