Ejemplo n.º 1
0
        private void FormLoad_Handler(object sender, EventArgs e)
        {
            // Execute SQL query to get Docente rows
            SqlCommand    cmd    = new SqlCommand("SELECT * FROM GestaoEscola.BlocoTipo", cn);
            SqlDataReader reader = cmd.ExecuteReader();
            // Create list of Objects given the query results
            List <BlocoTipo> tuplos = new List <BlocoTipo>();

            while (reader.Read())
            {
                BlocoTipo t = new BlocoTipo();
                t.codigo = Int32.Parse(reader["codigo"].ToString());
                t.nome   = reader["nome"].ToString();
                if (t.codigo > lastId)
                {
                    lastId = t.codigo;
                }
                tuplos.Add(t);
                counter++;
            }

            // Close reader
            reader.Close();

            // ObjectListView
            // Add Objects to list view
            listObjects.SetObjects(tuplos);

            // Update stats
            updateStats();
        }
Ejemplo n.º 2
0
        private void loadBlocosTipo()
        {
            // Execute SQL query to get Docente rows
            SqlCommand    cmd    = new SqlCommand("SELECT * FROM GestaoEscola.BlocoTipo", cn);
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                BlocoTipo t = new BlocoTipo();
                t.codigo = Int32.Parse(reader["codigo"].ToString());
                t.nome   = reader["nome"].ToString();
                panelFormFieldTipo.Items.Add(t.nome);
                tiposbloco.Add(t);
            }

            // Close reader
            reader.Close();
        }
Ejemplo n.º 3
0
        private void showObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            BlocoTipo f = (BlocoTipo)listObjects.SelectedObjects[0];

            // Set labels values
            panelObjectTitulo.Text    = "Bloco";
            panelObjectSubtitulo.Text = f.nome;
            // Show panel
            if (!panelObject.Visible)
            {
                panelObject.Visible = true;
            }
            panelForm.Visible = false;
        }
Ejemplo n.º 4
0
        private void editObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            BlocoTipo f = (BlocoTipo)listObjects.SelectedObjects[0];

            // Set textboxes value
            panelFormFieldNome.Text = f.nome;
            // Set title and description
            panelFormTitulo.Text    = "Editar tipo de bloco";
            panelFormDescricao.Text = "Altere os dados e submita o formulário";
            panelFormButton.Text    = "Submeter";
            // Make panel visible
            if (!panelForm.Visible)
            {
                panelForm.Visible = true;
            }
        }
Ejemplo n.º 5
0
        private void submitForm(Bloco bloco)
        {
            /*
             * If submition for edit t!=null
             * If new submition t==null
             */
            bool edit = (bloco != null);
            // Get form data
            String     nome        = panelFormFieldNome.Text;
            String     coordenadas = panelFormFieldCoordenadas.Text;
            BlocoTipo  blocotipo   = getBlocoTipo(panelFormFieldTipo.Text);
            NaoDocente supervisor  = getNaoDocente(panelFormFieldSupervisor.Text);
            // Create command
            String commandText = "INSERT INTO GestaoEscola.Bloco VALUES (@Coordenadas, @Nome, @Tipo, @Supervisor)";

            if (edit)
            {
                commandText = "UPDATE GestaoEscola.Bloco SET nome = @Nome, tipo = @Tipo, supervisor = @Supervisor WHERE coordenadas = @Coordenadas";
            }
            SqlCommand command = new SqlCommand(commandText, cn);

            // Add vars
            command.Parameters.Add("@Coordenadas", SqlDbType.VarChar).Value = coordenadas;
            command.Parameters.Add("@Nome", SqlDbType.VarChar).Value        = nome;
            command.Parameters.Add("@Tipo", SqlDbType.Int).Value            = blocotipo.codigo;
            command.Parameters.Add("@Supervisor", SqlDbType.Int).Value      = supervisor.nmec;
            // Execute query
            int rowsAffected = 0;

            try
            {
                rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected));
            }
            catch (SqlException ex)
            {
                String errorMessage = "Ocorreu um erro, verifique que preencheu todos os dados corretamente e tente novamente!";
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    if (ex.Errors[i].Message.IndexOf("Violation of PRIMARY KEY constraint", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        errorMessage = "Não pode inserir um bloco nas coordenadas de um já existente!";
                        break;
                    }
                }
                MessageBox.Show(
                    errorMessage + "\r\n\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If query is successful
            if (rowsAffected == 1)
            {
                // If add operation
                if (!edit)
                {
                    // Update lastId
                    lastId++;
                    // Add tuple to interface list
                    bloco             = new Bloco();
                    bloco.coordenadas = coordenadas;
                    bloco.nome        = nome;
                    bloco.tipo        = blocotipo;
                    bloco.supervisor  = supervisor;
                    listObjects.AddObject(bloco);
                }
                else
                {
                    // Get object on interface list and change attributes
                    bloco.nome       = nome;
                    bloco.tipo       = blocotipo;
                    bloco.supervisor = supervisor;
                }
                // SHow feedback to user
                String successMessage = "O bloco foi adicionado com sucesso!";
                if (edit)
                {
                    successMessage = "O bloco foi atualizado 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
                    );
            }
        }
Ejemplo n.º 6
0
        private void submitForm(BlocoTipo bloco)
        {
            /*
             * If submition for edit t!=null
             * If new submition t==null
             */
            bool edit = (bloco != null);
            // Get form data
            String nome = panelFormFieldNome.Text.Trim();
            // Create command
            String commandText = "INSERT INTO GestaoEscola.BlocoTipo VALUES (@ID, @Nome)";

            if (edit)
            {
                commandText = "UPDATE GestaoEscola.BlocoTipo SET nome = @Nome WHERE codigo = @ID";
            }
            SqlCommand command = new SqlCommand(commandText, cn);

            // Add vars
            command.Parameters.Add("@ID", SqlDbType.Int);
            if (edit)
            {
                command.Parameters["@ID"].Value = bloco.codigo;
            }
            else
            {
                command.Parameters["@ID"].Value = lastId + 1;
            }
            command.Parameters.Add("@Nome", SqlDbType.VarChar, 15);
            command.Parameters["@Nome"].Value = nome;
            // 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\r\n" + ex.ToString(),
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return;
            }
            // If query is successful
            if (rowsAffected == 1)
            {
                // If add operation
                if (!edit)
                {
                    // Update lastId
                    lastId++;
                    // Add tuple to interface list
                    bloco        = new BlocoTipo();
                    bloco.codigo = lastId;
                    bloco.nome   = nome;
                    listObjects.AddObject(bloco);
                }
                else
                {
                    // Get object on interface list and change attributes
                    bloco.nome = nome;
                }
                // SHow feedback to user
                String successMessage = "O tipo de bloco foi adicionado com sucesso!";
                if (edit)
                {
                    successMessage = "O tipo de bloco foi editado 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
                    );
            }
        }
Ejemplo n.º 7
0
        private void deleteObject()
        {
            // Get Object
            if (listObjects.Items.Count == 0 | current < 0)
            {
                return;
            }
            BlocoTipo f         = (BlocoTipo)listObjects.SelectedObjects[0];
            int       itemIndex = listObjects.SelectedIndex;
            // Confirm delete
            DialogResult msgb = MessageBox.Show("Tem a certeza que quer eliminar tipo de bloco'" + f.nome + "'?", "Esta operação é irreversível!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msgb == DialogResult.No)
            {
                return;
            }
            // Create command
            String     commandText = "pr_BlocoTiposDELETE";
            SqlCommand command     = new SqlCommand(commandText, cn);

            command.CommandType = CommandType.StoredProcedure;
            // Add vars
            command.Parameters.Add("@Codigo", SqlDbType.Int);
            command.Parameters["@Codigo"].Value = f.codigo;
            command.Parameters.Add("@Feedback", SqlDbType.VarChar, 4000).Direction = ParameterDirection.Output;
            // Return value stuff
            command.Parameters.Add("@ReturnVal", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

            // Execute query
            int    rowsAffected = 0;
            int    returnValue;
            String returnMessage = "";

            try
            {
                rowsAffected  = command.ExecuteNonQuery();
                returnValue   = (int)command.Parameters["@ReturnVal"].Value;
                returnMessage = (String)command.Parameters["@Feedback"].Value;
                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 && returnValue == 1)
            {
                // Remove object from interface list
                listObjects.Items.RemoveAt(itemIndex);
                // Show user feedback
                MessageBox.Show(
                    "O tuplo foi eliminado com sucess da base de dados!",
                    "Sucesso!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                // Update stats
                updateStats();
                // Hide panels
                panelForm.Visible   = false;
                panelObject.Visible = false;
            }
            else
            {
                String errorMessage = "Ocorreu um erro, tente novamente!";
                if (returnMessage.Contains("conflicted with the REFERENCE constraint \"FK"))
                {
                    errorMessage = "Este tipo de bloco não pode ser eliminado enquanto estiver atribuído a um bloco!";
                }
                MessageBox.Show(
                    errorMessage + "\r\n\r\n" + returnMessage,
                    "Erro!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }