Basic SQLite reading

Eduardo Gomez 4,156 Reputation points
2022-02-15T16:40:31.667+00:00

Hello

I have a basic app using sqlite, but is not reading

Class

   [PrimaryKey, AutoIncrement]  
        public int Id { get; set; }  
  
        public string Name { get; set; }  
  
        public float Ammount { get; set; }  
  
        [MaxLength(25)]  
        public string Descriion { get; set; }  
  
        public DateTime Date { get; set; }  
  
        public string Catergory { get; set; }  
  
        public Exenpse() { }  
  
        public static int InsertExpense(Exenpse exenpse) {  
  
            using (SQLiteConnection conn = new SQLiteConnection(App.DatbasePath)) {  
  
                conn.CreateTable<Exenpse>();  
                return conn.Insert(exenpse);  
            }  
        }  
  
        public static List<Exenpse> GetExpenses() {  
  
            using (SQLiteConnection conn = new SQLiteConnection(App.DatbasePath)) {  
  
                conn.CreateTable<Exenpse>();  
                return conn.Table<Exenpse>().ToList();  
            }  
        }  
    }  

ExpensesVM

 public ObservableCollection<Exenpse> Exenpses { get; set; }  
  
        public ICommand NewExpense { get; set; }  
  
        public ExpensesPageModel() {  
  
            Exenpses = new ObservableCollection<Exenpse>();  
  
            NewExpense = new Command(NewExpenseAction);  
  
            GetExpenses();  
  
        }  
  
  
        private void GetExpenses() {  
            var expense = Exenpse.GetExpenses();  
  
            Exenpses.Clear();  
            foreach (var ex in expense) {  
                Exenpses.Add(ex);  
            }  
        }  
  
        private void NewExpenseAction() {  
            App.Current.MainPage.Navigation.PushAsync(new NewExpensesPage());  
        }  

New Expences

      public ObservableCollection<string> Categories { get; set; }  
  
        public ICommand SaveCommand { get; set; }  
  
        public Exenpse Exenpse { get; set; }  
  
        public NewExpensePageModel() {  
            SaveCommand = new Command(SaveAction);  
  
            Categories = new ObservableCollection<string>();  
  
            Exenpse = new Exenpse();  
  
            GetCategories();  
  
        }  
  
        private void GetCategories() {  
  
            Categories.Add("Housing");  
            Categories.Add("Debt");  
            Categories.Add("Heath");  
            Categories.Add("Food");  
            Categories.Add("Travel");  
            Categories.Add("Other");  
        }  
  
        private void SaveAction() {  
  
            Exenpse = new Exenpse() {  
                Name = Exenpse.Name,  
                Ammount = Exenpse.Ammount,  
                Descriion = Exenpse.Descriion,  
                Date = Exenpse.Date,  
                Catergory = Exenpse.Catergory  
            };  
  
            int response = Exenpse.InsertExpense(Exenpse);  
  
            if (response > 0) {  
                Application.Current.MainPage.Navigation.PopAsync();  
            } else {  
                Application.Current.MainPage.DisplayAlert("Error", "No items were inserted", "OK");  
            }  
  
        }  
    }  

it doesn't read, I think it doesn't insert either

174604-screenshot-2022-02-15-185051.png

https://github.com/eduardoagr/Xamarin-Master-Class

I can see that I have 13 objects, inide the db

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-02-16T06:32:20.257+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Firstly, your GitHub page cannot be find.

    Based on your screenshot, you can get the data from the Sqlite DB, And you insert the Exenpse object successfully, But when you get these records, you get the empty or null result except the ID property, am I right?

    If so, this issue is related to your NewExpensePageModel.cs.

    You creata new object by Exenpse = new Exenpse(); in the NewExpensePageModel constructor, this Exenpse object do not have any data. But you save this empty Exenpse object in the SaveAction method with following code.

       Exenpse = new Exenpse()  
                   {  
                       Name = Exenpse.Name,  
                       Ammount = Exenpse.Ammount,  
                       Descriion = Exenpse.Descriion,  
                       Date = Exenpse.Date,  
                       Catergory = Exenpse.Catergory  
                   };  
    

    You can add following layout in your NewExpensesPage.xaml for testing. do not forget to add bindingContext for your NewExpensePageModel.cs

       <StackLayout>  
                   <Entry Text="{Binding Exenpse.Name}"></Entry>  
                   <Entry Text="{Binding Exenpse.Descriion}"></Entry>  
         
                   <Button Text="save" Command="{Binding SaveCommand}"></Button>  
               </StackLayout>  
    

    Then add Appearing event in the ExpensesPage.xaml

       <ContentPage.Behaviors>  
               <community:EventToCommandBehavior EventName="Appearing"  
                                                 Command="{Binding PageAppearCommand}" />  
           </ContentPage.Behaviors>  
    

    Add the PageAppearCommand and achieve this AppearAction method to get all of data when page appearing.

       public ICommand PageAppearCommand { get; set; }  
         
        public ExpensesPageModel()  
               {  
         
                   Exenpses = new ObservableCollection<Exenpse>();  
         
                   NewExpense = new Command(NewExpenseAction);  
                   PageAppearCommand = new Command(AppearAction);  
         
                   GetExpenses();  
         
               }  
               public virtual async void AppearAction()  
               {  
                   GetExpenses();  
               }  
    

    Here is a ebook about MVVM in Xamarin.Forms. you can read it.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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