How to update data to Mobile Services - Windows Phone (Windows Azure)
How to update data to Mobile Services - Windows Phone (Windows Azure) สรุปวิธีการบันทึก Update แก้ไขข้อมูลบน Table ของ Mobile Services บน Windows Azure ด้วย Windows Phone แบบสั้น ๆ ง่าย ๆ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;
namespace myPhoneApp
{
public partial class UpdatePage : PhoneApplicationPage
{
private MobileServiceCollection<MyMember, MyMember> items;
private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>();
private String id = "";
public UpdatePage()
{
InitializeComponent();
}
private async void InfoMember()
{
try
{
NavigationContext.QueryString.TryGetValue("sid", out id);
items = await memberTable
.Where(memberItem => memberItem.Id == Convert.ToInt32(id))
.ToCollectionAsync();
if (items.Count > 0)
{
MyMember member = items[0];
txtUsername.Text = member.Username.ToString();
txtPassword.Password = member.Password.ToString();
txtName.Text = member.Name.ToString();
txtEmail.Text = member.Email.ToString();
txtTel.Text = member.Tel.ToString();
}
else
{
MessageBox.Show("Not found Data!");
}
}
catch (MobileServiceInvalidOperationException ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InfoMember();
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
//** Get Item for Update ***/
items = await memberTable
.Where(memberItem => memberItem.Id == Convert.ToInt32(id))
.ToCollectionAsync();
MyMember member = items[0];
member.Name = txtName.Text;
member.Password = txtPassword.Password;
member.Name = txtName.Text;
member.Email = txtEmail.Text;
member.Tel = txtTel.Text;
//*** Update Record ***/
await memberTable.UpdateAsync(member);
MessageBox.Show("Update Data Successfully.");
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
catch (MobileServiceInvalidOperationException ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
}
}
Example
Show Case 3 : Update Data (Windows Phone and Mobile Services)