public Estudantes(SqlConnection cn, Form f) { this.cn = cn; this.previous = f; InitializeComponent(); this.nmec.GroupKeyGetter = delegate(object rowObject) { // When the same is returned by every object, all of them are put together in one group return("Número mecanográfico"); }; this.nome.GroupKeyGetter = delegate(object rowObject) { return("Nome"); }; this.nomeEE.GroupKeyGetter = delegate(object rowObject) { return("Nome Enc Educ"); }; this.telemovelEE.GroupKeyGetter = delegate(object rowObject) { // Group phones by the first two digits (phone company indicator) Estudante e = (Estudante)rowObject; if (e.telemovelEE.ToString().Length > 2) { return(e.telemovelEE.ToString().Substring(0, 2)); } return("Outros"); }; this.email_EE.GroupKeyGetter = delegate(object rowObject) { // Group emails by domain (text after @ symbol) Estudante e = (Estudante)rowObject; return(e.emailEE.Split('@')[1]); }; this.listObjects.FullRowSelect = true; //Make selection select the full row (and not only a cell) this.listObjects.SelectedIndex = 0; //Make the first row selected ad default // Filtering pesquisaAtributo.SelectedIndex = 0; // Set atribute combo box selected index to xero as default this.listObjects.UseFiltering = true; }
private void submitForm(Estudante e) { bool edit = (e != null); // Get form data int nmec = Int32.Parse(panelFormFieldNMec.Text); String nome = panelFormFieldNome.Text; String nomeEE = panelFormFieldNomeEE.Text; int tel = Int32.Parse(panelFormFieldContacto.Text); int telEE = Int32.Parse(panelFormFieldContactoEE.Text); String emailPrefixo = panelFormFieldEmail.Text.Split('@')[0]; String emailDominio = panelFormFieldEmail.Text.Split('@')[1]; String emailPrefixoEE = panelFormFieldEmailEE.Text.Split('@')[0]; String emailDominioEE = panelFormFieldEmailEE.Text.Split('@')[1]; // Create command String commandText = "GestaoEscola.EstudanteSP"; SqlCommand command = new SqlCommand(commandText, cn); command.CommandType = CommandType.StoredProcedure; // Add vars command.Parameters.Add("@NMec", SqlDbType.Int); command.Parameters["@NMec"].Value = nmec; command.Parameters.Add("@Nome", SqlDbType.VarChar); command.Parameters["@Nome"].Value = nome; command.Parameters.Add("@Telemovel", SqlDbType.Int); command.Parameters["@Telemovel"].Value = tel; command.Parameters.Add("@Email", SqlDbType.VarChar); command.Parameters["@Email"].Value = emailPrefixo; command.Parameters.Add("@EmailDominio", SqlDbType.VarChar); command.Parameters["@EmailDominio"].Value = emailDominio; command.Parameters.Add("@NomeEE", SqlDbType.VarChar); command.Parameters["@NomeEE"].Value = nomeEE; command.Parameters.Add("@TelemovelEE", SqlDbType.Int); command.Parameters["@TelemovelEE"].Value = telEE; command.Parameters.Add("@EmailEE", SqlDbType.VarChar); command.Parameters["@EmailEE"].Value = emailPrefixoEE; command.Parameters.Add("@EmailDominioEE", SqlDbType.VarChar); command.Parameters["@EmailDominioEE"].Value = emailDominioEE; command.Parameters.Add("@Edit", SqlDbType.Bit); if (edit) { command.Parameters["@Edit"].Value = 1; } command.Parameters["@Edit"].Value = 0; // Return value stuff var returnParameter = command.Parameters.Add("@ReturnVal", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; // Execute query int rowsAffected = 0; int returnValue; try { rowsAffected = command.ExecuteNonQuery(); returnValue = (int)returnParameter.Value; 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 >= 2 && returnValue == 1) { // If add operation, construct object (was null) if (!edit) { e = new Estudante(); } e.nmec = nmec; e.nome = nome; e.email = emailPrefixo + "@" + emailDominio; e.telemovel = tel; e.nomeEE = nomeEE; e.emailEE = emailPrefixoEE + "@" + emailDominioEE; e.telemovelEE = telEE; if (!edit) { listObjects.AddObject(e); } // SHow feedback to user String successMessage = "O estudante foi adicionado com sucesso!"; if (edit) { successMessage = "O estudante 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 ); } }
private void deleteObject() { // Get Object if (listObjects.Items.Count == 0 | current < 0) { return; } Estudante e = (Estudante)listObjects.SelectedObjects[0]; // Confirm delete DialogResult msgb = MessageBox.Show("Esta operação é irreversível!", "Tem a certeza que quer eliminar o estudante " + e.nmec.ToString() + "?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (msgb == DialogResult.No) { return; } // Delete tuple on db String commandText = "DELETE FROM GestaoEscola.Estudante WHERE NMec = @ID"; SqlCommand command = new SqlCommand(commandText, cn); // Add vars command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = e.nmec; // Execute query int rowsAffected = 0; try { rowsAffected = command.ExecuteNonQuery(); Console.WriteLine(String.Format("rowsAffected {0}", rowsAffected)); } catch (SqlException ex) { MessageBox.Show( "Ocorreu um erro, tente novamente!\r\n" + ex.ToString(), "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } // If successful query if (rowsAffected == 4) { // Remove object from interface list listObjects.RemoveObject(e); // Show user feedback MessageBox.Show( "O tuplo foi eliminado com sucesso da base de dados!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information ); // Update stats updateStats(); // Hide panels panelForm.Visible = false; panelObject.Visible = false; } else { MessageBox.Show( "Ocorreu um erro, tente novamente!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error ); } // Hide panels panelForm.Visible = false; panelObject.Visible = false; }