private void submitForm(Aula a) { bool edit = (a != null); // Get form data int sala = Int32.Parse(comboSala.SelectedItem.ToString()); string disciplina = comboDisciplina.Text; String nTurma = comboTurma.Text; TimeSpan horaInicio = TimeSpan.Parse(panelFormFieldHoraInicio.Text); TimeSpan horaFim = TimeSpan.Parse(panelFormFieldHoraFim.Text); int diaSem = comboDiaSemana.SelectedIndex + 1; // Create command SqlCommand command = new SqlCommand("GestaoEscola.AulaSP", cn); command.CommandType = CommandType.StoredProcedure; // Add vars command.Parameters.Add("@sala", SqlDbType.Int); command.Parameters["@sala"].Value = sala; command.Parameters.Add("@disciplina", SqlDbType.VarChar); command.Parameters["@disciplina"].Value = disciplina; command.Parameters.Add("@nomeT", SqlDbType.VarChar); command.Parameters["@nomeT"].Value = nTurma; command.Parameters.Add("@horaI", SqlDbType.Time); command.Parameters["@horaI"].Value = horaInicio; command.Parameters.Add("@horaF", SqlDbType.Time); command.Parameters["@horaF"].Value = horaFim; command.Parameters.Add("@diaS", SqlDbType.Int); command.Parameters["@diaS"].Value = diaSem; command.Parameters.Add("@docente", SqlDbType.Int); command.Parameters["@docente"].Value = d.nmec; command.Parameters.Add("@Edit", SqlDbType.Bit); command.Parameters["@Edit"].Value = 0; if (edit) { command.Parameters["@Edit"].Value = 1; } // Execute query int rowsAffected = 0; try { rowsAffected = command.ExecuteNonQuery(); Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected)); } catch (SqlException ex) { MessageBox.Show( "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!\r\n" + ex.ToString(), "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } // If query is successful if (rowsAffected == 1 || rowsAffected == 2) { // If add operation, construct object (was null) if (!edit) { a = new Aula(); } a.sala = sala; a.disciplina = disciplina; a.diaSemana = diaSem; a.turma = getTurma(nTurma); a.hInicio = horaInicio; a.hFim = horaFim; if (!edit) { listObjects.AddObject(a); } // SHow feedback to user String successMessage = "A aula foi adicionada com sucesso!"; if (edit) { successMessage = "A aula foi editada com sucesso"; } MessageBox.Show( successMessage, "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information ); // Update objects displayed on interface listObjects.BuildList(true); // Update stats updateStats(); // Hide panels panelForm.Visible = false; panelObject.Visible = false; } else { MessageBox.Show( "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
private void deleteObject() { // Get Object if (listObjects.Items.Count == 0 | current < 0) { return; } Aula f = (Aula)listObjects.SelectedObjects[0]; // Confirm delete DialogResult msgb = MessageBox.Show("Tem a certeza que pretende remover a aula da disciplina" + f.disciplina.ToString() + " deste docente?", "Esta operação é irreversível!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (msgb == DialogResult.No) { return; } SqlCommand command = new SqlCommand(" DELETE FROM GestaoEscola.Aula WHERE sala = @sala AND horaInicio = @hIn AND horaFim = @hFim", cn); command.Parameters.Add("@sala", SqlDbType.Int); command.Parameters["@sala"].Value = f.sala; command.Parameters.Add("@hIn", SqlDbType.Time); command.Parameters["@hIn"].Value = f.hInicio; command.Parameters.Add("@hFim", SqlDbType.Time); command.Parameters["@hFim"].Value = f.hFim; // Execute query int rowsAffected = 0; try { rowsAffected = command.ExecuteNonQuery(); Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected)); } catch (SqlException ex) { MessageBox.Show(ex.GetType().ToString()); MessageBox.Show( "Ocorreu um erro, tente novamente!\r\n\r\n" + ex.ToString(), "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } // If successful query if (rowsAffected == 1) { // Remove object from interface list listObjects.RemoveObject(f); // Show user feedback MessageBox.Show( "O tuplo foi eliminado com sucesso da base de dados!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information ); listObjects.BuildList(true); // Update stats updateStats(); // Hide panels panelForm.Visible = false; panelObject.Visible = false; } else { String errorMessage = "Ocorreu um erro, tente novamente!"; MessageBox.Show(errorMessage, "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); } }