protected void btnSalvar_Click(object sender, EventArgs e) { aluno.nome = txtNome.Text; if (txtIdade.Text != "") { aluno.idade = Convert.ToInt32(txtIdade.Text); } if (txtID.Text == "") { aluno.Salvar(); Aluno p = new Aluno(); p.idAluno = p.BuscaIdAluno(); //var p = new Aluno { idAluno = Aluno.BuscaIdAluno() }; if (Session["lista_de_materias"] != null) { var listaMateria = (HashSet <Aluno>)Session["lista_de_materias"]; foreach (var item in listaMateria) { p.idMateria = item.idMateria; p.nota1 = item.nota1; p.nota2 = item.nota2; p.SalvarNotaAluno(); } } Response.Write("<script>alert('Aluno " + txtNome.Text + " salvo com sucesso.');</script>"); Session.Remove("lista_de_materias"); } else { Aluno p = new Aluno(); p.idAluno = Convert.ToInt32(txtID.Text); if (Session["lista_de_materias"] != null) { // remove all records of the dataBase to avoid duplicates p.RemoveAllNotas(p.idAluno); // get all object of the session var lista = (HashSet <Aluno>)Session["lista_de_materias"]; foreach (var item in lista) { p.idMateria = item.idMateria; p.nota1 = item.nota1; p.nota2 = item.nota2; p.SalvarNotaAluno(); } } Session.Remove("lista_de_materias"); aluno.idAluno = Convert.ToInt32(txtID.Text); aluno.Alterar(); Response.Write("<script>alert('Professor " + txtNome.Text + " alterado com sucesso.');</script>"); } Response.Redirect("Listagem.aspx"); }