Beispiel #1
0
        private void BtnRegistrar_Click(object sender, EventArgs e)
        {
            int IdDireccion = 0;

            if (txtNombre.Text != string.Empty && txtApellidos.Text != string.Empty && txtCedula.Text != string.Empty &&
                txtCorreo.Text != string.Empty && txtTelefono.Text != string.Empty && txtDireccion.Text != string.Empty &&
                txtNumResidencia.Text != string.Empty && cbxSector.Text != string.Empty && cbxSucursal.Text != string.Empty &&
                txtUsuario.Text != string.Empty && txtPass1.Text != string.Empty && txtPass2.Text != string.Empty)
            {
                #region Checkers
                if (txtPass1.Text != txtPass2.Text)
                {
                    MessageBox.Show("Las contraseñas no coinciden.");
                    return;
                }
                if (!txtCedula.Text.All(char.IsDigit) || txtCedula.Text.Length != 11)
                {
                    MessageBox.Show("Cédula inválida.");
                    return;
                }
                if (!txtTelefono.Text.All(char.IsDigit) || txtTelefono.Text.Length != 10)
                {
                    MessageBox.Show("Teléfono inválido.");
                    return;
                }
                if (!txtNumResidencia.Text.All(char.IsDigit))
                {
                    MessageBox.Show("Número de residencia inválido.");
                    return;
                }
                #endregion

                using (var connDB = new SqlConnection(connStr))
                {
                    using (var command = connDB.CreateCommand())
                    {
                        //Insert Direccion
                        command.CommandText = @"
                            INSERT INTO tblDireccion (Calle, NumResidencia, IdSector)
                            OUTPUT inserted.IdDireccion
                            SELECT '" + txtDireccion.Text + "'," + txtNumResidencia.Text + ", IdSector FROM tblSector WHERE NombreSector = '" + cbxSector.Text + "';";
                        connDB.Open();

                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                IdDireccion = reader.GetInt32(0);
                            }
                        }

                        //Insert Persona
                        command.CommandText = @"
                            INSERT INTO tblPersona (Nombre, Apellidos, Usuario, Contrasena, Cedula, Email, Telefono, Estado, IdDireccion, IdSucursal, TipoPersona, FechaRegistro)
                            OUTPUT inserted.IdPersona
                            SELECT '" + txtNombre.Text + "', '" + txtApellidos.Text + "', '" + txtUsuario.Text + "', '" + txtPass1.Text + "', '" + txtCedula.Text + "', '" + txtCorreo.Text + "', " + txtTelefono.Text + ",'A', " + IdDireccion.ToString() + ", IdSucursal, 'C', '" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "' FROM tblSucursal WHERE NombreSucursal = '" + cbxSucursal.Text + "';";

                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                MessageBox.Show("¡Bienvenido a la Familia Phoenix!" + Environment.NewLine + "Tu ID Phoenix es " + (reader.GetInt32(0)).ToString());
                            }
                        }
                        connDB.Close();
                    }
                    this.Hide();
                    LogIn x = new LogIn();
                    x.ShowDialog();
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Por favor, llena todos los campos.");
            }
        }