FIll xamarin picker with sqlite table values

Fernando Manuel Gonçalves 41 Reputation points
2021-06-13T21:30:30.657+00:00

I an database with thie model
public class Encomenda
{

    [PrimaryKey, AutoIncrement]
    public int Id  { get; set; }
    public string EncID { get; set; }
    public System.DateTime EncData { get; set; }

}

Databse class with this

public Task<List<Encomenda>> GetEncomendaAsync()
{
return _database.Table<Encomenda>().ToListAsync();
}

On the view I have

picker.ItemsSource = (IList)App.Database.GetEncomendaAsync();

I get always an error "System.InvalidCastException: 'Specified cast is not valid.'

How can I solve this error?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-06-14T03:29:37.237+00:00

    Hi FernandoManuelGonalves-7609,

    Welcome to our Microsoft Q&A platform!

    I test both List<Encomenda> and IEnumerable<Encomenda>, and both of them work fine.

    Here is my Database class.

    class Database  
    {  
        public static SQLiteAsyncConnection db;  
        static async Task Init()  
        {  
            if (db != null)  
                return;  
      
            var databasePath = Path.Combine(FileSystem.AppDataDirectory, $"data.db");  
            db = new SQLiteAsyncConnection(databasePath);  
            await db.CreateTableAsync<Encomenda>();  
        }  
      
        public static async Task AddEncomenda(string encID, DateTime encData)  
        {  
            await Init();  
      
            var encomenda = new Encomenda  
            {  
                EncID = encID,  
                EncData = encData  
            };  
            await db.InsertAsync(encomenda);  
        }  
      
        public static async Task<IEnumerable<Encomenda>> GetEncomendaAsync()  
        {  
            await Init();  
            var encomenda = await db.Table<Encomenda>().ToListAsync();  
            var sortedencomenda = encomenda.OrderBy(x => x.EncData).ToList();  
            return sortedencomenda;  
        }  
    }  
    

    XAML:

    <StackLayout>  
        <Button Text="Get items" Clicked="Button_Clicked"/>  
        <Picker x:Name="MyPicker"  
                   ItemDisplayBinding="{Binding EncID}" />  
    </StackLayout>  
    

    xaml.cs

    private async void Button_Clicked(object sender, EventArgs e)  
    {  
        var items = await Database.GetEncomendaAsync();  
        MyPicker.ItemsSource = (System.Collections.IList)items;  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.