Beispiel #1
0
        private void appbarSave_Click(object sender, EventArgs e)
        {
            if ((String.IsNullOrEmpty(txtUser.Text) || String.IsNullOrWhiteSpace(txtUser.Text)))
            {
                MessageBox.Show(Resource.CompleteInformacion);
            }
            else
            {
                try
                {
                    using (ContextoDatos ctx = new ContextoDatos())
                    {

                        ctx.Users.Where(x => x.Id == Convert.ToInt32(Id)).SingleOrDefault().Nombre = txtUser.Text;

                        ctx.SubmitChanges();

                        MessageBox.Show(Resource.DataSaved);
                        NavigationService.Navigate(new Uri("/ListadoUsuarios.xaml?", UriKind.Relative));
                    }
                }
                catch (DbException x)
                {
                    MessageBox.Show("Error al actualizar: " + x.Message);
                }
            }
        }
        private void appbarDelete_Click(object sender, EventArgs e)
        {
            if (userSelected == null)
            {
                MessageBox.Show(Resource.EliminarNoSelected);
            }
            else
            {
                if (MessageBox.Show(Resource.EliminarMessageBoxText + " " + userSelected.Nombre, Resource.EliminarCaptionText, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    try
                    {
                        using (ContextoDatos ctx = new ContextoDatos())
                        {

                            var user        = ctx.Users.Where(x => x.Id == userSelected.Id).FirstOrDefault();
                            var userData    = ctx.Datas.Where(x => x.IdUsuario == userSelected.Id);

                            ctx.GetTable<DataUser>().DeleteAllOnSubmit(userData);
                            ctx.GetTable<User>().DeleteOnSubmit(user);

                            ctx.SubmitChanges();
                            loadUsers();
                        }
                    }
                    catch (DbException x)
                    {
                        MessageBox.Show("An error has occurred!, we worked to resolved this problem");
                    }
                }
            }
        }
Beispiel #3
0
        private void appbarSave_Click(object sender, EventArgs e)
        {
            var items = (List<Usuario>)listBox1.ItemsSource;

            var selectedItem = items != null ? items.Where(x => x.Selected).FirstOrDefault() : null;

            if ((String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrWhiteSpace(textBox1.Text)) && selectedItem == null)
                MessageBox.Show(Resource.CompleteInformacion);
            else
            {

                String Nombre = "";
                String Id = "";
                if (selectedItem != null)
                    Nombre = selectedItem.Nombre;
                else
                    Nombre = textBox1.Text;

                try
                {

                    if (selectedItem == null)
                    {

                        //ContextoDatos.DeleteDB();
                        ContextoDatos.CrearDBSiNoExiste();
                        User user = new User()
                        {
                            Nombre = textBox1.Text
                        };

                        DataUser datauser = new DataUser()
                        {
                            Altura = App.IsMetric ? Conversion.ConverDouble(dataAltura) : Conversion.ToCentimetros(Conversion.ConverDouble(dataAltura.Split('.').First()),
                                                                                                                 Conversion.ConverDouble(dataAltura.Split('.').Last())),
                            Edad = Convert.ToInt32(dataEdad),
                            Fecha =Convert.ToDateTime(datePicker1.Value),
                            Genero = dataGenero,
                            Indice = Conversion.ConverDouble(dataIndice),
                            //Peso = App.IsMetric ? Convert.ToDouble(dataPeso) : Conversion.ToKilogramos(Convert.ToDouble(dataPeso)),
                             Peso = App.IsMetric ? Convert.ToDouble(dataPeso) : Conversion.ToKilogramos(Convert.ToDouble(dataPeso)),

                            User = user
                        };

                        using (ContextoDatos ctx = new ContextoDatos())
                        {
                            ctx.GetTable<User>().InsertOnSubmit(user);
                            ctx.GetTable<DataUser>().InsertOnSubmit(datauser);
                            ctx.SubmitChanges();

                            Id = user.Id.ToString();
                        }
                    }
                    else
                    {
                        DataUser datauser = new DataUser();

                        datauser.Altura = App.IsMetric ? Conversion.ConverDouble(dataAltura)  : Conversion.ToCentimetros(Conversion.ConverDouble(dataAltura.Split('.').First()),
                                                                                                                 Conversion.ConverDouble(dataAltura.Split('.').Last()));
                        datauser.Edad	= Convert.ToInt32(dataEdad);
                        datauser.Fecha	= Convert.ToDateTime(datePicker1.Value);
                        datauser.Genero = dataGenero;
                        datauser.Indice = Conversion.ConverDouble(dataIndice);
                       // datauser.Peso	= App.IsMetric ? Convert.ToDouble(dataPeso)  : Conversion.ToKilogramos(Convert.ToDouble(dataPeso));
                        //datauser.Peso	= App.IsMetric ? Convert.ToDouble(dataPeso.Replace(",",".")): Conversion.ToKilogramos(Convert.ToDouble(dataPeso.Replace(",",".")));
                        datauser.Peso   = App.IsMetric?  Convert.ToDouble(dataPeso.Replace(System.Globalization.CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator.ToString(), ".")):Conversion.ToKilogramos(Convert.ToDouble(dataPeso));

                        using (ContextoDatos ctx = new ContextoDatos())
                        {
                            datauser.User = ctx.Users.Where(x => x.Id == selectedItem.Id).SingleOrDefault();

                            ctx.GetTable<DataUser>().InsertOnSubmit(datauser);
                            ctx.SubmitChanges();

                            Id = selectedItem.Id.ToString();
                        }
                    }

                    (App.Current as App).CurrentUser = null;
                    MessageBox.Show(Resource.DataSaved);
                    NavigationService.Navigate(new Uri("/DataUsers.xaml?Id=" + Id, UriKind.Relative));
                    //loadUsers();
                }
                catch (DbException x)
                {
                    MessageBox.Show("Error al insertar: " + x.Message);
                }
            }
        }