Pierre Eoe
Pierre Eoe

Reputation: 23

Can't read data from second table using Azure Mobile Services .NET

I created a new table "TravauxDBs" using Visual Studio, but when i call PullAsync it returns count = 0, so i guess it's empty. Then i get an error "not found".

Everything works fine using basic table ToDoItems, and i use the same code for this new table.

My item mobile side :

public class TravauxDB
{
    public string Id { get; set; }

    [JsonProperty(PropertyName = "nomtravail")]
    public string NomTravail { get; set; }

    [JsonProperty(PropertyName = "voie")]
    public string Voie { get; set; }

    [JsonProperty(PropertyName = "pk")]
    public string PK { get; set; }

    [JsonProperty(PropertyName = "quantity")]
    public string Quantity { get; set; }

    [JsonProperty(PropertyName = "unite")]
    public string Unite { get; set; }

    [JsonProperty(PropertyName = "observation")]
    public string Observation { get; set; }

    [JsonProperty(PropertyName = "idchantier")]
    public string IdChantier { get; set; }

}

Service side :

public class TravauxDB : EntityData
{
    public string NomTravail { get; set; }

    public string Voie { get; set; }

    public string PK { get; set; }

    public string Quantity { get; set; }

    public string Unite { get; set; }

    public string Observation { get; set; }

    public string IdChantier { get; set; }

}

Activity :

public class Travaux : ActionBarActivity
{
    private Android.Support.V7.Widget.SearchView _searchView3;
    private ListView _listView3;
    private ArrayAdapter _adapter3;

    private MobileServiceClient client;

    private IMobileServiceSyncTable<TravauxDB> TravauxTable;

    const string applicationURL = @"myurl";
    const string applicationKey = @"mykey";

    const string localDbFilename = "localstore.db";

    public List<string> travaux { get; private set; }

    protected override async void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Layout_travaux);

        CurrentPlatform.Init();

        client = new MobileServiceClient(applicationURL, applicationKey);
        await InitLocalStoreAsync();

        TravauxTable = client.GetSyncTable<TravauxDB>();

        await SyncAsync();

        List<string> travaux = await ItemsOnly();

        _listView3 = FindViewById<ListView>(Resource.Id.listView3);
        _adapter3 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, travaux);
        _listView3.Adapter = _adapter3;
        _listView3.ItemClick += _listView3_ItemClick;

    }

    void _listView3_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        int pos = e.Position;

        Intent b = new Intent(this, typeof(Travail));
        Bundle bundle = new Bundle();
        bundle.PutInt("Ntravail", pos);
        b.PutExtras(bundle);
        StartActivity(b);
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.main, menu);

        var item = menu.FindItem(Resource.Id.action_search);

        var searchItem = MenuItemCompat.GetActionView(item);
        _searchView3 = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();

        _searchView3.QueryTextChange += (s, e) => _adapter3.Filter.InvokeFilter(e.NewText);

        _searchView3.QueryTextSubmit += (s, e) =>
        {
            Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
            e.Handled = true;
        };

        return true;
    }

    private void CreateAndShowDialog(Exception exception, String title)
    {
        CreateAndShowDialog(exception.Message, title);
    }

    private void CreateAndShowDialog(string message, string title)
    {
        Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this);

        builder.SetMessage(message);
        builder.SetTitle(title);
        builder.Create().Show();
    }

    private async Task InitLocalStoreAsync()
    {
        string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename);

        if (!File.Exists(path))
        {
            File.Create(path).Dispose();
        }

        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<TravauxDB>();

        await client.SyncContext.InitializeAsync(store);
    }

    private async Task SyncAsync()
    {
        try
        {
            await client.SyncContext.PushAsync();
            await TravauxTable.PullAsync("alltravaux", TravauxTable.CreateQuery()); // query ID is used for incremental sync
        }
        catch (Java.Net.MalformedURLException)
        {
            CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
        }
        catch (Exception e)
        {
            CreateAndShowDialog(e, "Error");
        }
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
    }

    public async Task<List<string>> ItemsOnly()
    {
        try
        {

            List<string> travaux = await TravauxTable
                .Select(tr => tr.NomTravail).ToListAsync();
            return travaux;

        }
        catch (MobileServiceInvalidOperationException e)
        {
            Console.Error.WriteLine(@"ERROR {0}", e.Message);
            return null;
        }
    }

I also added a new controller for this, just replaced ToDoItem by TravauxDB :

public class TravauxController : TableController<TravauxDB>
{

    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        ChantierContext context = new ChantierContext();
        DomainManager = new EntityDomainManager<TravauxDB>(context, Request, Services);
    }

    // GET tables/TodoItem
    public IQueryable<TravauxDB> GetAllTravauxDB()
    {
        return Query();
    }

    // GET tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult<TravauxDB> GetTravauxDB(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task<TravauxDB> PatchTravauxDB(string id, Delta<TravauxDB> patch)
    {
        return UpdateAsync(id, patch);
    }

    // POST tables/TodoItem
    public async Task<IHttpActionResult> PostTravauxDB(TravauxDB item)
    {
        TravauxDB current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteTravauxDB(string id)
    {
        return DeleteAsync(id);
    }

}

If something is missing or you need more details let me know.

Thank you by advance.

Upvotes: 2

Views: 298

Answers (1)

lindydonna
lindydonna

Reputation: 3875

The client class name needs to match the server controller name. So, your class should be called Travaux instead of TravauxDB. Alternatively, you can use the attribute [TableName] on the class.

Upvotes: 1

Related Questions