0

I'm just getting started using Azure Storage. I've been following this nifty tutorial using Xamarin.Android. Here's what I'm trying to do...

I want to create a mobile app that will take users' names and email addresses using EditText fields and save them in a table in an Azure Storage Account. Here's the code I wrote:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Android.Preferences;

namespace UndergroundSports
{
    [Activity]          
    public class austinBowlingAthletesList : Activity
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);

            SetContentView (Resource.Layout.austinBowlingAthletesList);

            EditText austinBowlingFullNameEntry = FindViewById<EditText> (Resource.Id.austinBowlingFullNameEntry);

            EditText austinBowlingEmailEntry = FindViewById<EditText> (Resource.Id.austinBowlingEmailEntry);

            Button austinBowlingSubmitButton = FindViewById<Button> (Resource.Id.austinBowlingSignUpButton);

            CloudStorageAccount storageaccount = CloudStorageAccount.Parse ("StorageConnectionString");

            CloudTableClient tableClient = storageaccount.CreateCloudTableClient ();

            CloudTable austinBowlingAthletes = tableClient.GetTableReference ("austinBowlingAthletesTable");

            austinBowlingAthletes.CreateIfNotExistsAsync ();

            austinBowlingSubmitButton.Click += (sender, e) => {

                austinBowlingAthlete austinBowlingAthlete1 = new austinBowlingAthlete();
                austinBowlingAthlete1.fullname = austinBowlingFullNameEntry.ToString();
                austinBowlingAthlete1.email = austinBowlingEmailEntry.ToString();

                TableOperation insertOperation = TableOperation.Insert(austinBowlingAthlete1);

                austinBowlingAthletes.ExecuteAsync(insertOperation);

            };
        }

    }
}

Will this do what I want? It compiled error-free. However, I was expecting to see a table in the Table service section in my Storage Account in the Azure Portal after pressing the submit button. Instead, it says that I don't have any tables yet.

My first thought was that it's not really creating a table because it's only running in the emulator. Am I onto something? Or did I make a mistake somewhere along the line?

Let me know if you want me to give some more details or link any other parts of the solution. I really appreciate the help!

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
michael__sloan
  • 405
  • 2
  • 4
  • 14

1 Answers1

1

In your OnCreate method you call CreateIfNotExistsAsync but you don't await so there is race condition between button click and table creation. Await the call like this:

await austinBowlingAthletes.CreateIfNotExistsAsync();

You also need to await the call for inserting new item:

austinBowlingAthletes.ExecuteAsync(insertOperation);

You also need to make your method async:

    protected override async void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

        SetContentView (Resource.Layout.austinBowlingAthletesList);

        EditText austinBowlingFullNameEntry = FindViewById<EditText> (Resource.Id.austinBowlingFullNameEntry);

        EditText austinBowlingEmailEntry = FindViewById<EditText> (Resource.Id.austinBowlingEmailEntry);

        Button austinBowlingSubmitButton = FindViewById<Button> (Resource.Id.austinBowlingSignUpButton);

        CloudStorageAccount storageaccount = CloudStorageAccount.Parse ("StorageConnectionString");

        CloudTableClient tableClient = storageaccount.CreateCloudTableClient ();

        CloudTable austinBowlingAthletes = tableClient.GetTableReference ("austinBowlingAthletesTable");

        await austinBowlingAthletes.CreateIfNotExistsAsync ();

        austinBowlingSubmitButton.Click += async (sender, e) => {

            austinBowlingAthlete austinBowlingAthlete1 = new austinBowlingAthlete();
            austinBowlingAthlete1.fullname = austinBowlingFullNameEntry.ToString();
            austinBowlingAthlete1.email = austinBowlingEmailEntry.ToString();

            TableOperation insertOperation = TableOperation.Insert(austinBowlingAthlete1);

            await austinBowlingAthletes.ExecuteAsync(insertOperation);

        };
    }
Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • It gives me an error stating that "the await operator can only be used when its containing method is marked with the 'async' modifier." How can I get around this? And when I try to await the call for inserting the new item I get an error stating "the 'await' operator can only be used within an async lamda expression. Consider marking this lamda expression with the 'async' modifier. What should I do? – michael__sloan Apr 18 '16 at 15:23
  • @msloan11 Change your `OnCreate()` signature to: `protected override async void OnCreate(Bundle savedInstanceState)` and then change your button lambda signature to: `austinBowlingSubmitButton.Click += async (sender, e) => {` – hvaughan3 Apr 18 '16 at 15:29
  • That worked, but now the compiler is warning me that the call is not awaited. Should I await any of the methods? I did... – michael__sloan Apr 18 '16 at 15:41
  • @msloan11: Do you get warning about OnCreate not being awaited? – Giorgi Apr 18 '16 at 15:42
  • Yes. I awaited it and it went away. – michael__sloan Apr 18 '16 at 15:45
  • Should I have awaited it? – michael__sloan Apr 18 '16 at 15:46
  • @msloan11: No, you aren't calling OnCreate so you can't await it. It's called by the Android framework itself. – Giorgi Apr 18 '16 at 15:49
  • Okay, got it. Very helpful. I'll comment if I get it to work. Meanwhile, I'm having another problem http://stackoverflow.com/questions/36698509/settings-must-be-of-the-form-name-value-no-idea-what-to-do?noredirect=1#comment60985651_36698509 – michael__sloan Apr 18 '16 at 15:57