I am sorry for this re-post of a question, but the original was never answered in a way that I could understand and leverage. I was not certain if I should try to revive that question or spawn a new one.
I am attempting to add a Part object to my Part table within the database I created. I get the following error during execution
System.AggregateException
Message=One or more errors occurred. (Don't know about PartsManagement.Models.Part[])
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2027
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2759
at System.Threading.Tasks.Task.Wait () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2625
at PartsManagement.Database..ctor (System.String dbPath) [0x00027] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Database.cs:18
at PartsManagement.App.get_Database () [0x0000e] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\App.xaml.cs:20
at PartsManagement.Views.NewPartPage.Save_Clicked (System.Object sender, System.EventArgs e) [0x00057] in C:\Users\5w3rv0\source\repos\PartsManagement\PartsManagement\PartsManagement\Views\NewPartPage.xaml.cs:25
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <4ccdb3137d974856b786e1aeebbfbab6>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.71(intptr,intptr)
From the error message I have determined that the error appears to occur durring the execution of line 25
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
in the NewPartsPage.xaml.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using PartsManagement.Models;
namespace PartsManagement.Views
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class NewPartPage : ContentPage
{
public NewPartPage()
{
InitializeComponent();
}
async void Save_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(partName.Text) && !string.IsNullOrWhiteSpace(partDescription.Text))
{
await App.Database.SavePartAsync(new Part
{
Name = partName.Text,
Description = partDescription.Text
});
}
await Navigation.PopModalAsync();
}
async void Cancel_Clicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
}
}
}
Here is the Part class:
using SQLite;
namespace PartsManagement.Models
{
public class Part
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
and the database file:
using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;
using PartsManagement.Models;
namespace PartsManagement
{
public class Database
{
readonly SQLiteAsyncConnection _database;
public Database(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Part>().Wait();
_database.CreateTableAsync<Product>().Wait();
}
public Task<List<Part>> GetPartsAsync()
{
return _database.Table<Part>().ToListAsync();
}
public Task<int> SavePartAsync(Part aPart)
{
return _database.InsertAsync(aPart);
}
public Task<List<Product>> GetProductsAsync()
{
return _database.Table<Product>().ToListAsync();
}
public Task<int> SaveProductAsync(Part aProduct)
{
return _database.InsertAsync(aProduct);
}
}
}
The error seems to further indicate that the NewPartsPage cannot find the Part object, but I have referenced it with the proper using statement of using PartsManagement.Model;. I am not sure if the square brackets in the error are significant. Is it attempting to find a specific instantiation of the Parts object as an array? I have reviewed the sqlite key words, but Part does not seem to be included in it. Is my folder structure having an impact on the functionality I am looking for? I have performed this functionality in the local database tutorial, but this example is different in that I attempted to use a different folder structure. Not sure how this would impact, but just in case I am bringing it up.
This examples folder structure
static Database database;
public static Database Database
{
get
{
if (database == null)
{
database = new Database(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Inventory.db3"));
}
return database;
}
}
Including the product class as it is key to the root cause of this issue.
using SQLite;
namespace PartsManagement.Models
{
public class Product
{
[PrimaryKey, AutoIncrement]
public string ID { get; set; }
public string Name { get; set; }
public int Cost { get; set; }
public int OnHand { get; set; }
public int UsedIn { get; set; }
public double AveragePerProduct { get; set; }
public int NumberSold { get; set; }
public double Price { get; set; }
public Part[] PartsIn { get; set; }
}
}
it is failing when creating the Product
table because sqlite does not understand how to create an Part
array. If you want to have tables with FK relations you need to add the SQLite Extensions package