Xamarin Forms adding products to a database then retrieving the products and displaying them on a content page.

Matthew Zoljan 120 Reputation points
2023-05-02T12:00:08.9033333+00:00

Hi guys,

How to approach a Xamarin Forms app where I want to display a list of values from the database on a content page. I first need to add the list to the database which I want to do it manually in SQLITE.

I have an app where a user registers and selects values that is displayed in the app also. I have been able to display the values as well as update the values in the app once the user is logged in.

I now want a list of products to display in the List page, where the values that the user selected on previous pages is calculated against the product/list page. I first want to know how I add the products to the database where then I can display on a content page without user input. Is there a way to add products to a database and retrieve it using binding text?

I then want to multiple the values by user selection, but I just want to learn how I can get my values to display on a page from a database without typing them manually then I can learn how to increase/decrease the products based on the user values. I hope someone can lead me in the right direction.

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. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,396 Reputation points Microsoft Vendor
    2023-05-03T06:51:33.39+00:00

    Hello,

    Firstly, here is a document about Xamarin.Forms Local Databases(Sqlite) - Xamarin | Microsoft Learn. Here is a demo about SQLite, you can download and test it.

    I just want to learn how I can get my values to display on a page from a database without typing them manually

    Next, if you have tested above sqlite demo, you will find on data in DB, please open TodoListPage.cs and find OnAppearing method. You can add items with code when the DB is empty like following code.

    protected override async void OnAppearing()
            {
                base.OnAppearing();
               TodoItemDatabase database = await TodoItemDatabase.Instance;
               List<TodoItem>  todoItems=    await database.GetItemsAsync();
                if (todoItems == null || todoItems.Count == 0) {
                   for (int i = 0; i < 10; i++)
                    {
                        //Add items with code when List is empty
                        await database.SaveItemAsync(new TodoItem() { Name=i.ToString(), Notes="this Note"+i});
                    }
                
                }
                todoItems=await database.GetItemsAsync();          
                listView.ItemsSource = todoItems.OrderByDescending(item => item.ID).ToList();
            }
    

    then I can learn how to increase/decrease the products based on the user values.

    For example, I want the increase the products based on the User ID, we can use .OrderBy(item => item.ID) like following code

      todoItems.OrderBy(item => item.ID).ToList();
    
    

    Decreasing the products based on the User ID like following code.

    todoItems.OrderByDescending(item => item.ID).ToList();
    

    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 comments No comments

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.