简体   繁体   English

数据库 sqlite 中的项目未更新 (Xamarin.Forms)

[英]Items in database sqlite are not updating (Xamarin.Forms)

I am using Xamarin.Forms in visual studio and I have a list of products that I insert in a table.我在 Visual Studio 中使用 Xamarin.Forms 并且我有一个我在表格中插入的产品列表。 When I try to update the Cantidad, Precio or other feature, the Articulo don´t update and the list appears as it was before.当我尝试更新 Cantidad、Precio 或其他功能时,Articulo 不会更新,并且列表会像以前一样显示。 I´m trying to use the PK as a string so when I try to update a item I need the Producto of the Articulo, it verify that the Id of the Producto exist in the database and then update.我正在尝试将 PK 用作字符串,因此当我尝试更新项目时,我需要 Articulo 的 Producto,它会验证 Producto 的 Id 是否存在于数据库中,然后进行更新。

This is my Model Articulo.cs这是我的 Model Articulo.cs

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace Saansa.Modelos
{
    public class Articulo
    {
        [PrimaryKey,AutoIncrement]
        public string Id { get; set; }
        public string Producto { get; set; }
        public int Precio { get; set; }
        public int Cantidad { get; set; }
        public string MasterCategory { get; set; }
        public string Category1 { get; set; }
        public string Category2 { get; set; }
        public string Category3 { get; set; }
    }
}

This is my xalm file:这是我的 xalm 文件:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Saansa"
             x:Class="Saansa.Inventario"
             Title="Inventario">

            <StackLayout BackgroundColor="#f5cda2">
                <Image Margin="0,0,0,0" HeightRequest="10" Source="inventario.png" ></Image>
                <Label Margin="0,0,0,0" Text="Mi Cafe Delicias" FontAttributes="Bold"
                       FontSize="Large" TextColor="#b27b4b" HorizontalTextAlignment="Center" ></Label>
                <Entry x:Name="txtProducto" Placeholder="Código Producto"></Entry>
                <Entry x:Name="txtNombre" Placeholder="Nombre del producto"></Entry>
                <Entry x:Name="txtPrecio" Placeholder="Precio del producto"></Entry>
                <Entry x:Name="txtCantidad" Placeholder="Cantidad del Producto"></Entry>
                <Entry x:Name="txtMainCategory" Placeholder="Categoria general"></Entry>
                <Entry x:Name="txtSub1" Placeholder="Subcategoria 1"></Entry>
                <Entry x:Name="txtSub2" Placeholder="Subcategoria 2"></Entry>
                <Entry x:Name="txtSub3" Placeholder="Subcategoria 3"></Entry>
        <StackLayout  HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <Button x:Name="btnAdd" WidthRequest="200" Text="Añadir" Clicked="BtnAdd_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                    <Button x:Name="btnRead" WidthRequest="200" Text="Buscar" Clicked="BtnRead_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                </StackLayout>
                <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <Button x:Name="btnUpdate" WidthRequest="200" Text="Actualizar" Clicked="BtnUpdate_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                    <Button x:Name="btnDelete" WidthRequest="200" Text="Borrar" Clicked="BtnDelete_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                </StackLayout>
                <ListView x:Name="lstArticulo" BackgroundColor="#f5cda2">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout>
                                    <Label Text="{Binding Producto}"/>
                                    <Label Text="{Binding Id}" HorizontalOptions="EndAndExpand"
                                           TextColor="Black"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
</ContentPage>

In my Inventario.xalm.cs my update option is this:在我的 Inventario.xalm.cs 我的更新选项是这样的:

 private async void BtnUpdate_Clicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtProducto.Text))
            {
                Modelos.Articulo articulo = new Modelos.Articulo()
                {
                    Id = txtProducto.Text,
                    Producto = txtNombre.Text,
                    Precio = Convert.ToInt32(txtPrecio.Text),
                    Cantidad = Convert.ToInt32(txtCantidad.Text),
                    MasterCategory = txtMainCategory.Text,
                    Category1 = txtSub1.Text,
                    Category2 = txtSub2.Text,
                    Category3 = txtSub3.Text
                };

                //Update Person
                await App.SQLiteDb.SaveItemAsync(articulo);

                txtNombre.Text = string.Empty;
                txtCantidad.Text = string.Empty;
                txtProducto.Text = string.Empty;
                txtPrecio.Text = string.Empty;
                txtMainCategory.Text = string.Empty;
                txtSub1.Text = string.Empty;
                txtSub2.Text = string.Empty;
                txtSub3.Text = string.Empty;
                await DisplayAlert("Success", "Person Updated Successfully", "OK");
                //Get All Persons
                var articuloLista = await App.SQLiteDb.GetItemsAsync();
                if (articuloLista != null)
                {
                    lstArticulo.ItemsSource = articuloLista;
                }

            }
            else
            {
                await DisplayAlert("Required", "Please Enter Nombre articulo", "OK");
            }
        }


and my SQLiteHekper.cs:和我的 SQLiteHekper.cs:

public Task<int> SaveItemAsync(Modelos.Articulo articulo)
        {   //articulo.Cantidad. !string.IsNullOrEmpty(articulo.Cantidad)
            if (!string.IsNullOrEmpty(articulo.Id))
            {
                //probando esto 
                //var nuevoArticulo = GetItemAsync(articulo.Producto);
                return db.UpdateAsync(articulo);
            }  
            else
            {
                return db.InsertAsync(articulo);
            }
        }

The error is that it was not getting the object, thats why first we need to call it and then edit the Articulo that we want to update.错误是它没有得到 object,这就是为什么我们首先需要调用它然后编辑我们想要更新的 Articulo。

Private async void BtnUpdate_Clicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtNombre.Text))
        {
            Modelos.Articulo articulo = await App.SQLiteDb.GetItemAsync(txtNombre.Text);

            //Aparentemente dejar el texto vacío no es equivalente a "", por eso el +"0"
            articulo.Cantidad = (string.Equals("0", txtCantidad.Text + "0")) ? articulo.Cantidad : Convert.ToInt32(txtCantidad.Text);
            articulo.Category1 = (string.Equals("0", txtSub1.Text + "0")) ? articulo.Category1 : txtSub1.Text;
            articulo.Category2 = (string.Equals("0", txtSub2.Text + "0")) ? articulo.Category2 : txtSub2.Text;
            articulo.Category3 = (string.Equals("0", txtSub3.Text + "0")) ? articulo.Category3 : txtSub3.Text;
            articulo.MasterCategory = (string.Equals("0", txtMainCategory.Text + "0")) ? articulo.MasterCategory : txtMainCategory.Text;
            articulo.Precio = (string.Equals("0", txtPrecio.Text + "0")) ? articulo.Precio : Convert.ToInt32(txtPrecio.Text);
            articulo.Costo = (String.Equals("0", txtCosto.Text + "0")) ? articulo.Costo : Convert.ToInt32(txtCosto.Text);
            articulo.Producto = (string.Equals("0", txtProducto.Text + "0")) ? articulo.Producto : txtNombre.Text;

            await App.SQLiteDb.SaveItemAsync(articulo);

            txtNombre.Text = string.Empty;
            txtCantidad.Text = string.Empty;
            txtProducto.Text = string.Empty;
            txtCosto.Text = string.Empty;
            txtPrecio.Text = string.Empty;
            txtMainCategory.Text = string.Empty;
            txtSub1.Text = string.Empty;
            txtSub2.Text = string.Empty;
            txtSub3.Text = string.Empty;
            await DisplayAlert("Success", "Producto actualizado con éxito", "OK");


        }
        else
        {
            await DisplayAlert("Required", "Please Enter código articulo", "OK");
        }
    }

This makes to bring the object first, check if some data is not in the entry to let it be like it is and finally it updates the object.这使得首先带来object,检查是否某些数据不在条目中以使其保持原样,最后更新object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM