I want to fetch first record from AboutUs table and display as a content label.
I have created 4 classes in MVVM pattern.
First is Model class AboutUs.cs
[Table("tblAboutUs")] public class AboutUs { [PrimaryKey, AutoIncrement, NotNull] public int IDP { get; set; } public string Content { get; set; } }
Second is Data Access class
SQLiteAboutUs.cs
public class SQLiteAboutUs { private static readonly AsyncLock Mutex = new AsyncLock(); private SQLiteAsyncConnection dbConn; public int StatusCode { get; set; } public SQLiteAboutUs(ISQLitePlatform sqlitePlatform, string dbPath) { if (dbConn == null) { var connectionFunc = new Func<SQLiteConnectionWithLock>(() => new SQLiteConnectionWithLock ( sqlitePlatform, new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false) )); dbConn = new SQLiteAsyncConnection(connectionFunc); dbConn.CreateTableAsync<Model.AboutUs>(); } } public SQLiteAboutUs() { } public async Task Save(Model.AboutUs content) { using (await Mutex.LockAsync().ConfigureAwait(false)) { StatusCode = 0; await dbConn.InsertAsync(new Model.AboutUs { Content = content.Content }); StatusCode = 1; } //For Get first Row from Table public async Task<Model.AboutUs> GetAllData() { return await dbConn.Table<Model.AboutUs>().Where(x => x.IDP == 1).FirstOrDefaultAsync(); } }
Third class ViewModel Class
AboutUsViewModel.cs
public class AboutUsViewModel { readonly SQLiteAboutUs _db; public string AboutUsContent { get; set; } //public string AboutUs; public AboutUsViewModel() { _db = new SQLiteAboutUs(); } public async void FirstRecord() { Model.AboutUs obj = await _db.GetAllData(); this.AboutUsContent = obj.Content; } }
Forth one is Code behind file of my xaml pages.
AboutUs.xaml.cs
public partial class AboutUs : ContentPage { readonly AboutUsViewModel aboutUsViewModel; public AboutUs() { InitializeComponent(); aboutUsViewModel = new AboutUsViewModel(); aboutUsViewModel.FirstRecord(); lblContent.Text = aboutUsViewModel.AboutUsContent; } }
I have debug code but problem is In AboutUsViewModel.cs class in FirstRecord Method object can not be set that's why AboutUsContent string property is also not set.
I can't figure out why my debugger directly jump from GetAllData() method in SQLiteAboutUs.cs to label.text in code behind file of view?