How to insert data to Mobile Services - Windows Phone (Windows Azure)
How to insert data to Mobile Services - Windows Phone (Windows Azure) สรุปวิธีการบันทึก Insert ข้อมูลเข้า 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 myPhoneApp.Resources;
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;
namespace myPhoneApp
{
public class MyMember
{
public int Id { get; set; }
[JsonProperty(PropertyName = "username")]
public string Username { get; set; }
[JsonProperty(PropertyName = "password")]
public string Password { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "tel")]
public string Tel { get; set; }
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
private MobileServiceCollection<MyMember, MyMember> items;
private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>();
// Constructor
public MainPage()
{
InitializeComponent();
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
items = await memberTable.ToCollectionAsync();
var insertItem = new MyMember { Username = txtUsername.Text
, Password = txtPassword.Password
, Name = txtName.Text
, Email = txtEmail.Text
, Tel = txtTel.Text
};
await memberTable.InsertAsync(insertItem);
items.Add(insertItem);
MessageBox.Show("Register Data Successfully.");
}
catch (MobileServiceInvalidOperationException ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
}
}
Example
Show Case 1 : Register Form (Windows Phone and Mobile Services)