We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Bind sqlite database to combobox

Thread ID:

Created:

Updated:

Platform:

Replies:

149881 Dec 12,2019 08:07 PM UTC Jul 10,2020 02:54 PM UTC Xamarin.Forms 4
loading
Tags: SfComboBox
Boris Oprit
Asked On December 12, 2019 08:07 PM UTC

How do i bind a combobox to a sqlite database ?

In the database there a some table's but i want to bind the combobox to "Zeeboot"


namespace Notes.Data
{
    public class NoteDatabase
    {
        readonly SQLiteAsyncConnection _database;

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

        public Task<List<Note>> GetNotesAsync()
        {
            return _database.Table<Note>().ToListAsync();
        }

        public Task<Note> GetNoteAsync(int id)
        {
            return _database.Table<Note>()
                            .Where(i => i.ID == id)
                            .FirstOrDefaultAsync();
        }

        public Task<int> SaveNoteAsync(Note note)
        {
            if (note.ID != 0)
            {
                return _database.UpdateAsync(note);
            }
            else
            {
                return _database.InsertAsync(note);
            }
        }

        public Task<int> DeleteNoteAsync(Note note)
        {
            return _database.DeleteAsync(note);
        }
    }
}

And


namespace Notes.Models
{
    public class Note
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Zeeboot { get; set; }
        public string Schip { get; set; }
        public string Stad { get; set; }
        public DateTime Date { get; set; }
    }
}




Sakthivel Palaniyappan [Syncfusion]
Replied On December 16, 2019 02:21 AM UTC

Hi Boris,

 

Greetings from Syncfusion.

 

We are currently working in the sample. We will update the details by 17th  December 2019.

 

Please let us know if you have any concern.

 

Regards,

Sakthivel P.



Hemalatha Marikumar [Syncfusion]
Replied On December 17, 2019 09:15 AM UTC

Hi Boris,

Greetings from Syncfusion.

We have analyzed your query. You can bind the SQLite database to ComboBox datasource as like below code snippet.

Data base:
 
 
public class ComboBoxDataBase 
    { 
        static object locker = new object(); 
        ISQLiteService SQLite 
        { 
            get 
            { 
                return DependencyService.Get<ISQLiteService>(); 
            } 
        } 
        readonly SQLiteConnection connection; 
        readonly string DatabaseName; 
  
        public ComboBoxDataBase(string databaseName) 
        { 
            DatabaseName = databaseName; 
            connection = SQLite.GetConnection(DatabaseName); 
        } 
  
        public void CreateTable<T>() 
        { 
            lock (locker) 
            { 
                connection.CreateTable<T>(); 
            } 
        } 
  
        public int SaveItem<T>(T item) 
        { 
            lock (locker) 
            { 
                var id = ((Employee)(object)item).ID; 
  
                return connection.Insert(item); 
  
            } 
        } 
        public IEnumerable<T> GetItems<T>() where T : new() 
        { 
            lock (locker) 
            { 
                return (from i in connection.Table<T>() select i).ToList(); 
            } 
        } 
  
        public int DeleteAll<T>() 
        { 
            lock (locker) 
            { 
                return connection.DeleteAll<T>(); 
            } 
        } 
    } 

XAML:
 
 
<combobox:SfComboBox   
 x:Name="comboBox" 
 DataSource="{Binding EmployeeCollection}" 
 DisplayMemberPath="ZeeBoot" /> 

MainPage:
 
  public partial class MainPage : ContentPage 
    { 
        ViewModel comboBoxData = new ViewModel(); 
        public ObservableCollection<Employee> EmployeeCollection { getset; } 
        public MainPage() 
        { 
            InitializeComponent(); 
            EmployeeCollection = new ObservableCollection<Employee>(); 
  
            var employee = comboBoxData.ItemsSource.GetItems<Employee>(); 
  
            foreach (Employee emp in employee) 
            { 
                EmployeeCollection.Add(emp); 
            } 
  
            comboBox.BindingContext = this; 
        } 
    } 

We have created sample based on your requirement, please find the sample from below location.

Sample link: 
https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxSample-1068743880.zip

Please let us know whether the above solution full fill your requirement. 
 
Regards, 
Hemalatha M. 


James
Replied On July 9, 2020 04:55 PM UTC

Hemalatha,

I am currently trying to implement your solution. What is the purpose of the following? Specifically the Delete employee table part and the creation entries.

My table is already filled with data I don't want to delete. 

James

  public ViewModel()
        {
            ItemsSource = new ComboBoxDataBase("DataSource");
            ItemsSource.CreateTable<Employee>();
            ItemsSource.DeleteAll<Employee>();
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "eric" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Jessie" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Lisa" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Jack" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Tom" });
        }


Suganya Sethuraman [Syncfusion]
Replied On July 10, 2020 02:54 PM UTC

Hi James, 
 
We have to create a collection locally and assign the values by retrieving from the database. By using “DisplayMemberPath” you can set ZeeBoot string to be displayed. 
 
Regards, 
Suganya Sethuraman 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon