예제 #1
0
파일: Form1.cs 프로젝트: sacaia/GreenCity
        private void btnLogar_Click(object sender, EventArgs e)
        {
            // Se algum campo estiver vazio, o usuario sera advertido
            if (txbNome.Text == "" || txbSenha.Text == "")
            {
                MessageBox.Show("Preencha todos os campos!");
                return;
            }
            try
            {
                SqlConnection con = new SqlConnection();
                cs = cs.Substring(cs.IndexOf("Data Source"));
                con.ConnectionString = cs;

                // verifica os parametros passados pelo usuario no formulario
                string     cmd_s = "select id from usuario where nome=@nome and senha=@senha";
                SqlCommand cmd   = new SqlCommand(cmd_s, con);

                // @nome = nome escolhido pelo usuario
                cmd.Parameters.AddWithValue("@nome", txbNome.Text);

                // @senha = senha escolhida pelo usuario
                cmd.Parameters.AddWithValue("@senha", Encriptador.Encrypt(txbSenha.Text));

                con.Open();

                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet        ds    = new DataSet();

                adapt.Fill(ds);
                con.Close();

                if (ds.Tables[0].Rows.Count == 1)
                {
                    // existe o usuario com essa senha
                    frmPrincipal frmP = new frmPrincipal();
                    this.Hide();
                    // frmP.FormClosed += (s, arg) => this.Show();
                    frmP.Show();
                }
                else
                {
                    MessageBox.Show("Senha ou Usuário Incorretos");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            // Limpa a tela
            txbNome.Text  = "";
            txbSenha.Text = "";
        }
예제 #2
0
파일: Form2.cs 프로젝트: sacaia/GreenCity
        private void btnCadastro_Click(object sender, EventArgs e)
        {
            // Se algum campo estiver vazio, o usuario sera advertido
            if ((txbNome.Text == "" || txbSenha.Text == "" || txbConfirmarSenha.Text == ""))
            {
                MessageBox.Show("Preencha todos os campos!");
                return;
            }
            // Se as senhas nao corresponderem, o usuario sera advertido
            if (!(txbSenha.Text == txbConfirmarSenha.Text))
            {
                MessageBox.Show("Senhas diferrentes!");
                return;
            }
            try
            {
                SqlConnection con = new SqlConnection();
                cs = cs.Substring(cs.IndexOf("Data Source"));
                con.ConnectionString = cs;

                // Insere os valores passados pelo usuario no formulario
                string     cmd_s = "insert into usuario values(@nome, @senha, 0)";
                SqlCommand cmd   = new SqlCommand(cmd_s, con);

                // @nome = nome escolhido pelo usuario
                cmd.Parameters.AddWithValue("@nome", txbNome.Text);

                // @senha = senha escolhida pelo usuario
                cmd.Parameters.AddWithValue("@senha", Encriptador.Encrypt(txbSenha.Text));

                con.Open();

                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet        ds    = new DataSet();

                adapt.Fill(ds);
                con.Close();

                // Se chegar ate aqui sem nenhum problema, da um feedback pro usuario
                MessageBox.Show("Registrado com susseso");
                frmLogin frmL = new frmLogin();
                frmL.Show();
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            // Limpa a tela para a possivel insercao de outro registro
            txbNome.Text           = "";
            txbSenha.Text          = "";
            txbConfirmarSenha.Text = "";
        }