Good Day Everyone. I'm creating Xamarin.Forms Portable Application in my Visual Studio 2015.
I want my Mobile Application to connect to the SQL Database I have in my VS2015 and return a LIST OF CUSTOMERS which should be display to my mobile phone.
In my solution, I have created a Xamarin Portable project and a WebForms project that will handle my Web Services and Database.
In my WebForms project, I was able to retrieve data from the Database using LINQ expression. I even checked this by using WEB API and I was really able to retrieved data. Like the image below :
What I want to do is to access this data to my Xamarin Portable project using RestClient. I used the WebServiceUrl of my WebForms project to get the data it contains. But whenever I used a Breakpoint to test if it retrieved the Data, it does not return any value.
Meaning I won't really be able to display the LIST OF CUSTOMERS.
What do you think is the reason behind this?
Here are some of my codes :
1.) WebForms
CustomerController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebFormsDemo;
using WebFormsDemo.ViewModel;
namespace WebFormsDemo.Controllers
{
    public class CustomerController : ApiController
        {
    private EBMSEntities db = new EBMSEntities();
    // GET: api/Customer
    public IQueryable<CustomerViewModel> GetCustomerViewModels()
    {
        var displaycustomerInfo = from cust in db.CUSTOMERs
                                  select new CustomerViewModel
                                  {
                                      Id = cust.CUSTOMER_ID,
                                      CUSTOMER_CODE = cust.CUSTOMER_CODE,
                                      CUSTOMER_NAME = cust.CUSTOMER_NAME,
                                      CUSTOMER_MOBILE_NUMBER = cust.CUSTOMER_MOBILE_NUMBER,
                                      CUSTOMER_EMAIL_ADDRESS = cust.CUSTOMER_EMAIL_ADDRESS,
                                      CUSTOMER_CONTACT = cust.CUSTOMER_EMAIL_ADDRESS + "," + " " + cust.CUSTOMER_MOBILE_NUMBER
                                  };
        return displaycustomerInfo;
        }
    }
}
2.) XamarinForms
RestClient.cs
public class RestClient_Customer <T>
{
    private const string WebServiceUrl = "http://localhost:50857/api/Customer/";
    public async Task<List<T>> GetCustomerAsync()
    {
        var httpClient = new HttpClient();
        var json = await httpClient.GetStringAsync(WebServiceUrl);
        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
        return taskModels;
    }
  }
.
CustomerServices.cs
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
    public class CustomerServices
    {
    public async Task<List<Customer>> GetCustomerAsync()
    {
        RestClient_Customer<Customer> restClient = new RestClient_Customer<Customer>();
        var customerList = await restClient.GetCustomerAsync(); //yung getasync ay pantawag as restclient
        return customerList;
        }
    }
}
.
CustomerVM.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
using XamarinFormsDemo.Views;
namespace XamarinFormsDemo.ViewModels
{
public class CustomerVM : INotifyPropertyChanged
{
    private List<Customer> _customerList; // keep all customers
    private List<Customer> _searchedCustomerList; // keep a copy for searching
    private Customer _selectedCustomer = new Customer();
    private string _keyword = "";
    public string Keyword
    {
        get
        {
            return _keyword;
        }
        set
        {
            this._keyword = value;
            // while keyword changed we filter Employees
            //Filter();
        }
    }
    private void Filter()
    {
        if (string.IsNullOrWhiteSpace(_keyword))
        {
            CustomerList = _searchedCustomerList;
        }
        else
        {
            // var lowerKeyword = _keyword.ToLower();
            CustomerList = _searchedCustomerList.Where(r => r.CUSTOMER_NAME.ToLower().Contains(_keyword.ToLower())).ToList();
            //  EmployeesList = _searchedEmployeesList.Where(r => r.EMPLOYEE_NAME.Contains(_keyword)).ToList();
        }
    }
    public List<Customer> CustomerList
    {
        get
        {
            return _customerList;
        }
        set
        {
            _customerList = value;
            OnPropertyChanged();
        }
    }
    public CustomerVM()
    {
        InitializeDataAsync();
    }
    private async Task InitializeDataAsync()
    {
        var customerServices = new CustomerServices();
        _searchedCustomerList = await customerServices.GetCustomerAsync();
        CustomerList = await customerServices.GetCustomerAsync();
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    }
}
NOTE: Please take a closer look to my RestClient whether I'm doing it right thing or not. Thanks a lot.

 
     
    