public static List <Eleicao_DTO> RetornarEleicao() { try { List <Eleicao_DTO> datas = new List <Eleicao_DTO>(); string script = "SELECT TOP 5 data_eleicao, descricao, ativa FROM Eleicao WHERE ativa = 'Agendada' OR ativa = 'Ativa' ORDER BY data_eleicao"; SqlCommand cmd = new SqlCommand(script, Conexao_DAL.Conexao()); SqlDataReader dados = cmd.ExecuteReader(); while (dados.Read()) { Eleicao_DTO obj = new Eleicao_DTO(); obj.DataEleicao = dados["data_eleicao"].ToString(); obj.Nome = dados["descricao"].ToString(); obj.Status = dados["ativa"].ToString(); datas.Add(obj); } return(datas); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (Conexao_DAL.Conexao().State != ConnectionState.Closed) { Conexao_DAL.Conexao().Close(); } } }
public static string ValidarEleicao(Eleicao_DTO obj) { if (string.IsNullOrWhiteSpace(obj.Nome)) { throw new Exception("Campo Nome vazio!"); } try { DateTime data = Convert.ToDateTime(obj.DataEleicao); if (data >= DateTime.Today) { string data_certa = data.ToShortDateString(); return(CadEleicao_DAL.CadastrarEleicao(obj, data_certa)); } else { throw new Exception("Data Inválida!"); } } catch (Exception ex) { throw new Exception(ex.Message); } }
private void btnCriar_Click(object sender, EventArgs e) { try { Eleicao_DTO obj = new Eleicao_DTO(); mTxtBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals; if (mTxtBox.Text == string.Empty) { throw new Exception("Campo Data vazio!"); } mTxtBox.TextMaskFormat = MaskFormat.IncludeLiterals; obj.DataEleicao = mTxtBox.Text; obj.Nome = textBox1.Text; string msg = CadEleicao_BLL.ValidarEleicao(obj); MessageBox.Show(msg, "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information); mTxtBox.Clear(); textBox1.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static string CadastrarEleicao(Eleicao_DTO obj, string data) { try { string script = "INSERT INTO Eleicao (data_eleicoes, descricao, ativa ) VALUES (@Data, @Descricao, 'Agendada')"; SqlCommand cmd = new SqlCommand(script, Conexao_DAL.Conexao()); cmd.Parameters.AddWithValue("@Data", data); cmd.Parameters.AddWithValue("@Descricao", obj.Nome); cmd.ExecuteNonQuery(); return("Eleição cadastrada com sucesso!"); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (Conexao_DAL.Conexao().State != ConnectionState.Closed) { Conexao_DAL.Conexao().Close(); } } }