vendredi 25 mars 2016

Fail to insert data on SQLite, WP8.1 app C#, MVVM pattern

I'm actually developing a Windows 8.1 app, it's a small app for a simple store. I'm using MVVM pattern, and SQLite for storage. When I'm trying to insert some rows just for testing, app skips this step and don't add my rows.

Here's my XAML, where I'm calling my ViewModel:

<ListBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2"
             Margin="15,0" ItemsSource="{Binding Products}"
             Background="Black" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled">

My page initializes:

public ListProducts()
    {
        this.InitializeComponent();
        ListProductsVM ListProductsOnView = new ListProductsVM();
        if (ListProductsOnView.CreateCommand.CanExecute(null))
        ListProductsOnView.CreateCommand.Execute(null);
    }

My CreateCommand ICommand, and Products list:

public ICommand CreateCommand
    {
        get { return this._createCommand; }
    }
    public ObservableCollection<ProductsVM> Products
    {
        get { return this.products; }
        set { this.SetProperty(ref this.products, value); }
    }
    private async void Create_Executed()
    {
        await DAL.CreateDatabase();
    }

And this is where DB should be created with three rows:

public static async Task CreateDatabase()
    {
        var result = await Checkdatabase();
        if (!result)
        {
            // Create a new connection
            using (var db = DbConnection)
            {
                // Active tracing
                db.TraceListener = new DebugTraceListener();

                // Create the tables if they does not exist
                var prod = db.CreateTable<Product>();
                var sale = db.CreateTable<Sales>();
                var kard = db.CreateTable<Kardex>();
                var infoprod = db.GetMapping(typeof(Product));
                var infosale = db.GetMapping(typeof(Sales));
                var infokard = db.GetMapping(typeof(Kardex));

            // TODO Delete this values
                Product product = new Product();
                product.ID = 1;
                product.Name = "Galletas";
                product.Unit = "Unidad";
                product.Cost = 12.00;
                product.Price = 15.00;
                //product.Active = true;

                var i = db.InsertOrReplace(product);

                product = new Product();
                product.ID = 2;
                product.Name = "Leche";
                product.Unit = "Litros";
                product.Cost = 8.00;
                product.Price = 10.00;
                product.Active = false;

                i = db.InsertOrReplace(product);

                product = new Product();
                product.ID = 3;
                product.Name = "Orégano";
                product.Unit = "Unidad";
                product.Cost = 0.20;
                product.Price = 0.30;
                product.Active = true;

                i = db.InsertOrReplace(product);
            }
        }

    }

Actually, my project is on GitHub, if you want to check it detailed: http://ift.tt/1RAGOA8

It's exactly skipping this:

var i = db.InsertOrReplace(product);

It should set this new product on my model, but it doesn't.

What do you think the problem could be? Thanks to everybody.

Aucun commentaire:

Enregistrer un commentaire