Database problem C#

Asked

Viewed 55 times

0

I have a problem with C# which is as follows: I would like when I selected a combo box option, which comes directly from the database, to appear the respective code of the one I selected in a label on the right side of the combo box. I’ve done this in a different exercise before, but I can’t figure out where the problem is. My teacher said that a solution would be to use the igualartext() method, but it still doesn’t work. In case, only with this system select an option and appear a label according to the selected that I would complete the exercise, which is to record what I selected in the bank.

Here is the code:

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 frm_reg_men : Form
{

OleDbConnection conn = Conexao.obterConexao();

OleDbConnection dr_Alunos;
BindingSource bs_Alunos = new BindingSource();

OleDbConnection dr_Disciplinas;
BindingSource bs_Disciplinas = new BindingSource();

OleDbConnection dr_Mencao;
BindingSource bs_Mencao = new BindingSource();

OleDbConnection dr_reg_notas;
BindingSource bs_reg_notas = new BindingSource();

String _query;

public frm_reg_men()
{
    InitializeComponent();
}

private void carregar_aluno()
{
    _query = "SELECT * from Alunos order by Nome";
    OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
    OleDbDataReader dr_Alunos = _dataCommand.ExecuteReader();

    if (dr_Alunos.HasRows == true)
    {
        bs_Alunos.DataSource = dr_Alunos;
        cmb_aluno.DataSource = bs_Alunos;
        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_disciplina()
{
    _query = "SELECT * from Disciplinas order by sigla";
    OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
    OleDbDataReader dr_Disciplinas = _dataCommand.ExecuteReader();
    if (dr_Disciplinas.HasRows == true)
    {
        bs_Disciplinas.DataSource = dr_Disciplinas;
        cmb_disciplina.DataSource = bs_Disciplinas;
        cmb_disciplina.DisplayMember = "sigla";
        cmb_disciplina.ValueMember = "cod_disciplina";
        lbl_codigo.Text = cmb_disciplina.SelectedValue.ToString();
    }
    else
    {
        MessageBox.Show("Não temos Disciplinas Cadastrados !!!!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    }
}

private void carregar_mencao()
{
    _query = "SELECT * from Mencao order by Mencao";
    OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
    OleDbDataReader dr_Mencao = _dataCommand.ExecuteReader();
    if (dr_Mencao.HasRows == true)
    {
        bs_Mencao.DataSource = dr_Mencao;
        cmb_mencao.DataSource = bs_Mencao;
        cmb_mencao.DisplayMember = "Mencao";
        cmb_mencao.ValueMember = "Mencao";

    }
    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);
    OleDbDataReader 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 frm_reg_men_Load(object sender, EventArgs e)
{
    carregar_grid();
    carregar_aluno();
    carregar_disciplina();
    carregar_mencao();
}

private void button1_Click(object sender, EventArgs e)

    {
        _query = "Insert into Registro_Mencoes (matricula,cod_disciplina,mencao) Values ";
        _query += "('" + lbl_matricula.Text + "','" + lbl_codigo.Text + "','" + cmb_mencao.Text + "')";
        try
        {
            OleDbCommand _dataCommand = new OleDbCommand(_query, conn);
            _dataCommand.ExecuteNonQuery();
            carregar_grid();
            MessageBox.Show("Incluido com sucesso!!", "Inclusão", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        catch (Exception)
        {
            MessageBox.Show("Problemas com a Inclusão!!", "Inclusão", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

public void igualar_text()
{
    lbl_matricula.DataBindings.Add("Text", bs_Alunos, "Matricula");
    lbl_matricula.DataBindings.Clear();
    lbl_codigo.DataBindings.Add("Text", bs_Alunos, "Disciplinas");
    lbl_codigo.DataBindings.Clear();

}


}

}

Databases: inserir a descrição da imagem aqui

  • Where this called the equal_text?

  • 1

    Recommendation: STOP learning Webforms, you are wasting your time. There is no market for this. Learn ASPNET MVC.

  • 1

    Webforms? I think this is Winforms and I believe it is worth learning to then pass to WPF. I would just recommend learning the development patterns correctly. On the question, you should use any of the events: selectedindexchanged , valuechanged or selectionchanged.

  • OleDbConnection dr_Alunos; I think this should be a DataReader and not a Connection

  • In Windowsforms, what I would do is add a Databinding in Combobox, and in Selectedindexchanged event, I would take what’s in Selectedvalue, if I put it in Binding.

No answers

Browser other questions tagged

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