locked
How to ACCESS DATABASE in XAMARIN.FORMS? RRS feed

  • Question

  • User208792 posted

    Good Day everyone. I'm having trouble on how am I going to show all the records I have created in my ASP.NET WEB API to Xamarin.Forms Application. I tried creating pre-defined list of Employee's Name and Department and it worked. But what I want to do is Create a Record in ASP.NET Web Application and make it appear to my mobile application. Any help will be highly appreciated. Here's my code. I'm just a newbie so please forgive me if my question isn't seem to be informative. Thanks in advance. I watch a video tutorial regarding this matter._ Refer to this link if needed. _ https://www.youtube.com/watch?v=Lir75oNAeiM

    MainViemMain.xaml `

         <?xml version="1.0" encoding="utf-8" ?>
                     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                                  x:Class="XamarinFormsDemo.MainPageMain"
                                  xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo">
    
    
                       <ContentPage.BindingContext>
                         <ViewModels:MainViewModel/>
                       </ContentPage.BindingContext>
    
          <StackLayout>
          <ListView ItemsSource="{Binding EmployeesList}"
                    HasUnevenRows="True">
            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <StackLayout Orientation="Vertical"
                               Padding="12,6">
                    <Label Text="{Binding Name}"
                           FontSize="24"/>
    
                    <Label Text="{Binding Department}"
                           FontSize="18"
                           Opacity="0.6"/>
                  </StackLayout>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
    
          </ListView>
    
          <Label Text="Read This!"/>
    
          </StackLayout>
    
        </ContentPage>`
    

    MainViewModel.cs

        `using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Linq;
        using System.Runtime.CompilerServices;
        using System.Text;
        using System.Threading.Tasks;
        using XamarinFormsDemo.Models;
        using XamarinFormsDemo.Services;
    
        namespace XamarinFormsDemo.ViewModels
        {
            public class MainViewModel : INotifyPropertyChanged
            {
    
                private List<Employee> _employeesList;
    
                public List<Employee> EmployeesList
                {
                    get { return _employeesList; }
                    set
                    {
                        _employeesList = value;
                        OnPropertyChanged();
                    }
                }
    
                public MainViewModel()
                {
                    InitializeDataAsync();
                }
    
                private async Task InitializeDataAsync()
                {
                    var employeesServices = new EmployeesServices();
    
                    EmployeesList = await employeesServices.GetEmployeesAsync();
                }
    
    
    
                public event PropertyChangedEventHandler PropertyChanged;
    
                protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                {
                    var handler = PropertyChanged;
                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
                }
    
    
            }
        }`
    

    EmployeesServices.cs

                `
                using Plugin.RestClient;
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using XamarinFormsDemo.Models;
    
                namespace XamarinFormsDemo.Services
                {
                    public class EmployeesServices
                    {
    
    
                        public async Task<List<Employee>> GetEmployeesAsync()
                        {
                            RestClient<Employee> restClient = new RestClient<Employee>();
    
                            var employeesList = await restClient.GetAsync();
    
                            return employeesList;
    
                        }
    
    
                    }
                }
                `
    
    Friday, June 17, 2016 4:37 AM

Answers

  • User60112 posted

    Like you have this code here public List<Employee> EmployeesList { get { return _employeesList; } set { _employeesList = value; OnPropertyChanged(); } }

    go to the like line _employeesList = value; and set a break point (or press F9) . Then debug the application (i.e. press F5) , If the process stops at where you put the break point then that means it has reached to this point .

    and @MaxMeng has already given you an example of what he meant by the way . But I think you need to focus more on the advanced fundamentals of programming in C# more so things become easier for you :)

    protected override async void OnAppearing() { await this.ViewModel.InitializeDataAsync(); }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, June 25, 2016 8:24 AM

All replies

  • User60112 posted

    Assuming that you are receiving this list from the service , instead of using a List List<Employee> EmployeesList change it to ObservableCollection<Employee> EmployeesList

    Friday, June 17, 2016 12:52 PM
  • User208792 posted

    @ahmadmadi Thanks for the answer Sir. But it didn't work? :(

    Monday, June 20, 2016 9:07 AM
  • User60112 posted

    have you done the binding two ways ? ItemsSource="{Binding EmployeesList, Mode=TwoWays}"

    Monday, June 20, 2016 9:09 AM
  • User208792 posted

    @ahmadmadi I've also tried that. But still it didn't worked :(

    Tuesday, June 21, 2016 2:41 AM
  • User92384 posted

    Do not do this public MainViewModel() { InitializeDataAsync(); }

                private async Task InitializeDataAsync()
                {
                    var employeesServices = new EmployeesServices();
    
                    EmployeesList = await employeesServices.GetEmployeesAsync();
                }
    

    Instead, in page's onAppearing, call await mainViewModel.InitializeDataAsync().

    And if it's still not work, could you try manually set the listview's itemssource to mainViewModel.EmployeesList?

    Tuesday, June 21, 2016 4:30 AM
  • User60112 posted

    Can you see if the Setter of EmplyeesList is hit ? like put a breakpoint on the set part and see if it is changed and it gets values

    Tuesday, June 21, 2016 7:46 AM
  • User208792 posted

    @MaxMeng "Instead, in page's onAppearing, call await mainViewModel.InitializeDataAsync().

    And if it's still not work, could you try manually set the listview's itemssource to mainViewModel.EmployeesList?"

    What do you mean by this? How can I do that? I've tried to create manually a listview, and it worked. But that's not how it supposed to be.

    Friday, June 24, 2016 8:02 AM
  • User92384 posted

    @JohnChristian avoid calling an async method in constructor, you will lost control.

    After you bind this viewmodel to your page, override page's OnAppearing method, under that call your viewmodel's InitializeDataAsync, this should work.

    for instance, protected override async void OnAppearing() { await this.ViewModel.InitializeDataAsync(); }

    Friday, June 24, 2016 9:46 AM
  • User208792 posted

    @ahmadmadi How can I do that Sir?

    Saturday, June 25, 2016 4:56 AM
  • User208792 posted

    @MaxMeng what do you mean override OnAppearing method? Where can I find that? Should I create another method? Can you please show me the exact codes?

    Saturday, June 25, 2016 6:41 AM
  • User208792 posted

    @MaxMeng what do you mean "override page's OnAppearing method"? How can I do that? Can you show me your exact code please?

    Saturday, June 25, 2016 6:50 AM
  • User60112 posted

    Like you have this code here public List<Employee> EmployeesList { get { return _employeesList; } set { _employeesList = value; OnPropertyChanged(); } }

    go to the like line _employeesList = value; and set a break point (or press F9) . Then debug the application (i.e. press F5) , If the process stops at where you put the break point then that means it has reached to this point .

    and @MaxMeng has already given you an example of what he meant by the way . But I think you need to focus more on the advanced fundamentals of programming in C# more so things become easier for you :)

    protected override async void OnAppearing() { await this.ViewModel.InitializeDataAsync(); }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, June 25, 2016 8:24 AM
  • User208792 posted

    @ahmadmadi Alright. Thanks a lot Man.

    Thursday, June 30, 2016 2:46 AM
  • User331408 posted

    During the accessing of your MS Access Database if somehow your Access database form gets corrupted. Then you can manually fix this issue just by trying out the following fixes.

    Backup and Restore the Database

    For this you need to have proper backup of the Access Database to restore from. So, all the MS Access application users should always stick with this backing up plan. Everyone should do when the data involved is critical and worthy. Here are the steps to be followed: 1. Delete all the rows your backup and then copy that data with the help of AOD (VBA Code). 2. Make use of the “Import Wizard” option to import entire data tables. To do this open a page in new database which you have created as follows: o In the Database window click on Objects -> Pages o Tap to the New option o Choose “Existing Web Page”

    SOURCE: http://www.accessrepairnrecovery.com/blog/recover-deleted-form-in-access-database

    Thursday, March 7, 2019 12:24 PM