Specified cast is not Valid

Asked

Viewed 138 times

0

I’m developing a C#academic registration app. I have some composers of objects like:

public class Turma
{
    public String Descricao { get; set; }
    public int QtdAlunos { get; set; }
    public Turno turno { get; set; }
    public Curso curso { get; set; }
    public Semestre semestre { get; set; }
    public Nivel nivel { get; set; }
    public int Ano { get; set; }
    public String Sala { get; set; }
}

The difficulty and the following, I have a procedure research in SqlServer receiving parameters of type int of objects: Turno, Curso, Semestre and Nivel. I populated the comboBox with their respective objects as follows the example:

public static void Nivel()
{
    NivelController nivelController = new NivelController();
    NivelColecao nivelColecao = nivelController.NivelConsultar();

    cboNivel.DataSource = null;
    cboNivel.DataSource = nivelColecao;

    cboNivel.DisplayMember = "Descricao";
    cboNivel.ValueMember = "IDNivel";

    cboNivel.Update();
    cboNivel.Refresh();
}

So.. What should happen is that in selectedIndex of ComboBox should update the DataGridView.

private void cboNivel_SelectedIndexChanged(object sender, EventArgs e)
{
        turma.nivel = new Nivel();
        turma.nivel.IDNivel = (int)cboNivel.SelectedValue;
}

The error is as follows:

Specifiedcast is not Valid and Unable to cast Object of type 'Model.Nivel' to type 'System.Iconvertible'.

publi  void Turma(Turma turma) 
{
    try
    {
        TurmaController turmaController = new TurmaController();
        TurmaColecao turmaColecao = turmaController.TurmaConsultar(turma);

        var TurmaListar = turmaColecao.Select(Turma => new
        {
            Turma = Turma.Descricao,
            QtdAlunos = Turma.QtdAlunos,
            Sala = Turma.Sala,
            Ano = Turma.Ano,
            Curso = Turma.curso.Descricao,
            Turno = Turma.turno.Descricao,
            Nivel = Turma.nivel.Descricao,
            Semestre = Turma.semestre.Descricao

        }).ToList();

        dGV.DataSource = null;
        dGV.DataSource = TurmaListar;

        dGV.AutoResizeColumns();

        dGV.Update();
        dGV.Refresh();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Erro ao fazer a consulta. Detalhes: " + ex.Message);
    }
} 
  • In its last code is "publi".

  • was a failure to copy the code here, but that is not what is infecting the error

No answers

Browser other questions tagged

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