Figer's Technology Consulting | January 2015

Azure Mobile Services Async in .NET Web Forms applications



It's easy to code in Javascript or Java and catch the successful asyncinsert, but in web forms the proper code wasn't obvious nor easily found online. Here is my solution to add a record to my Azure Mobile Service on web forms button click. The key is the line in bold.


        protected void btnAdd_Click(object sender, EventArgs e)

        {

            RegisterAsyncTask(new PageAsyncTask(() => SaveRecord()));

        }


        public async Task SaveRecord()

        {

            //Get Domain name from username (which is email address)

            String[] strUsername = User.Identity.Name.Split('@');

            String strUserDomain = strUsername[1];


            try

                var sunshineTable = client.GetTable<sunshine>();


                var SunshineRecord = new sunshine

                {

                    firstname = this.txtFirstName.Text,

                    lastname = this.txtLastName.Text,

                    email = this.txtEmail.Text,

                    userdomain = strUserDomain

                };


                await sunshineTable.InsertAsync(SunshineRecord);

                String strID = SunshineRecord.Id;


                //Sunshine Record Saved Successfully, now lets save group NPI records

                    var npirecords = client.GetTable<npirecords>();

                    var npirecord = new npirecords();

                    NPIDatatable dtNPIrecords = (NPIDatatable)Session["NPIGroupRecords"];

                    foreach (DataRow row in dtNPIrecords.lNPIDatatable.Rows) // Loop over the rows.

                    {


                        //coveredrecipienttype = this.ddlCoveredRecipientType.SelectedValue,

                        npirecord.firstname = row["First Name"].ToString();

                        npirecord.lastname = row["Last Name"].ToString();

                        npirecord.npi = row["NPI"].ToString();

                        npirecord.credentials = row["Credentials"].ToString();

                        npirecord.gender = row["Gender"].ToString();

                        npirecord.address = row["Address"].ToString();

                        npirecord.city = row["City"].ToString();

                        npirecord.state = row["State"].ToString();

                        npirecord.zip = row["Zip"].ToString();

                        npirecord.country = row["Country"].ToString();

                        npirecord.licensenumber = row["License Number"].ToString();

                        npirecord.licensestate = row["License State"].ToString();

                        npirecord.taxonomycode = row["Taxonomy Code"].ToString();

                        npirecord.sunshinerecordid = strID;

                        await npirecords.InsertAsync(npirecord);

                    };

             }

            catch (Exception ex)

            {

    

            }

        }