0

I wanted to store and retrieve an array in a SQLite database in my Xamarin App. I'm following this Xamarin Tutorial on SQLite.

These values will be then show in Picker. I used Models earlier to store array and then get the values and list it in Picker, it was working fine, but the only problem was that it wasn't stored permanently (by closing app array list get empty).

Banks

using SQLite;

namespace Mobile.App
{
    public class Banks
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Name { get; set; }
        public string Number { get; set; }
    }
}

Database.cs

public class Database
{
    readonly SQLiteAsyncConnection _database;

    public Database(string dbPath)
    {
        _database = new SQLiteAsyncConnection(dbPath);
        _database.CreateTableAsync<SQLiteBank>().Wait();
    }

    public Task<List<Banks>> GetBanksAsync(string name)
    {
        return _database.Table<Banks>().ToListAsync();
    }

    public Task<int> SaveBanksAsync(Banks bank)
    {
        return _database.InsertAsync(bank);
    }
}

ViewModel.cs

Store and retrieve the array list in the database. When trying to save the array into the database, I get an error:

Cannot implicitly convert type 'string[]' to 'string'

When trying to retrieve the array from the database, I get an error:

Cannot convert from 'System.Collections.Generic.List<Mobile.App.Banks>' to 'string'

// Array of Bank Name and Number

string[] Name = new string[] { "Bank 1", "Bank 2", "Bank 3" };
string[] Number = new string[] { "000000", "111111", "222222" };

// Save it in database

await App.Database.SaveBanksAsync(new Banks
{
Name = Name, // Cannot implicity convert typer 'string[]' to 'string'
Number = Number // // Cannot implicity convert typer 'string[]' to 'string'
});

var bankName = await App.Database.GetBanksAsync(Name);

_bankList.Add(bankName); // Cannot convert from 'System.Collections.Generic.List<Mobile.App.Banks>' to 'string'

And then get the values in Picker

private ObservableCollection<string> _bankList = new ObservableCollection<string>();
public ObservableCollection<string> BankList
{
    get
    {
        return _bankList;
    }
    set
    {
        this.RaiseAndSetIfChanged(ref _bankList, value);
    }
}
<Picker 
    x:Name="picker_bank" 
    Title="Select Bank"
    ItemsSource="{Binding BankList}"
    SelectedItem="{Binding SelectedBank}">
</Picker>
3
  • what is BankModel? Commented Jan 16, 2021 at 12:03
  • BankModel is a model, public static string[] Name { get; set; }.. I can also use directly to initialize the array... Commented Jan 16, 2021 at 12:10
  • i edited it, changed it to string[] Name and string[] Number... Commented Jan 16, 2021 at 12:15

1 Answer 1

3

instead of doing this

string[] Name = new string[] { "Bank 1", "Bank 2", "Bank 3" };
string[] Number = new string[] { "000000", "111111", "222222" };

try this

List<Banks> banks = new List<Banks>();

// repeat for each bank
banks.Add(new Banks { Name = "Bank1", Number = "00000" });

then you can save them to your db

foreach(var bank in banks)
{
    await App.Database.SaveBanksAsync(bank);
}

next, this doesn't work

var bankName = await App.Database.GetBanksAsync(Name);

because GetBanksAsync returns all banks, not just the name of one bank.

BankList should be an ObservableCollection<Banks>, not string. Then you can do this

// load all banks into a list
var banks = await App.Database.GetBanksAsync();
// create an observablecollection from that list
BankList = new ObservableCollection<Banks>(banks);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for response, now in Picker, there are 6 times Mobile.App.Banks is printed? and I just want to print the Name of Banks in Picker...
use the ItemDisplayBinding property to tell it which property of your model you want to display - see learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
ok, thanks alot.. and if I want to update the number of specific bank e.g. update the bank 10's number to 101010.. how can I do this?
and can I open the sqlite file? and I can't find it, looked into android/data, there is nothing in there..
update the property on the appropriate Banks object and save it to the db - this is well documented

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.