Mobile Zone is brought to you in partnership with:

I am mastered in computer application (MCA)and working as Architect in one of the leading software services company in India. I have more than 7 years of experience in software development. Sumit is a DZone MVB and is not an employee of DZone and has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Phone: Save, Update and Delete Using Web API

07.26.2012
| 2248 views |
  • submit to reddit

In one of the previous article I talked about how to consume Web API in Windows Phone. In this article I will talk about how to use Web API from Windows Phone to Save, Update and Delete.

Like my previous article, In this article also I will use JSON.NET

Let's write code.

Step 1: Download code from ASP.NET MVC 4 - Hello Web API or you can create Web API application following the steps.

Step 2: Add below code in the CustomerController.cs which is present in the Controllers folder dowloaded from above step.

public Customer DeleteCustomer(Customer customer)
{
   //Code to delete customer
   return new Customer { CustomerId = 100, CustomerName = "Hemant", Address = "Bangalore" };
}

public Customer PostCustomer(Customer customer)
{
   //Code to save or update customer
   return customer;
}

 Step 3: Download JSON.NET from CodePlex

Step 4: Unzip downloaded JSON.NET, browse to WindowsPhone folder. You will get Newtonsoft.Json.dll in the folder.

Windows Phone JSON.NET

Step 5: Create Silverlight for Windows Phone project.

Step 6: Place two textblocks, two textboxes and two buttons in MainPage.Xaml inside contentpanel.  One button to delete and other button to Save or Update.

<TextBlock Text="Name" Height="30" HorizontalAlignment="Left" Margin="10,100,0,0" Name="textDefault" VerticalAlignment="Top" FontSize="18" />
<TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,90,0,0" Name="txtText" Text="" VerticalAlignment="Top" Width="320" />

<TextBlock Text="Address" Height="30" HorizontalAlignment="Left" Margin="10,175,0,0" Name="textEntropy" VerticalAlignment="Top" FontSize="18" />
<TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,165,0,0" Name="txtEntropy" Text="" VerticalAlignment="Top" Width="320" />

<Button x:Name="btnSaveData" Grid.Row="0" Height="75" Content="Save Data" Click="btnSaveData_Click" />
<Button x:Name="btnDeleteData" Grid.Row="0" Margin="12,200,12,0" Height="75" Content="Delete Data" Click="btnDeleteData_Click" />

Step 7: Create a Class Customer with the entities you like to bind. The Web API Json will have CustomerId, CustomerName and Address entities

The name of entities in Customer class should be same as the name of entities returned by Web API in Json.

public class Customer
{
   public int CustomerId { get; set; }
   public string CustomerName { get; set; }
   public string Address { get; set; }
}

 Step 8: Add reference of Newtonsoft.Json.dll from WindowsPhone folder and add Newtonsoft.Json and System.Text using directive.

using Newtonsoft.Json;
using System.Text;

Step 9: Place btnDeleteData_Click method in MainPge.Xaml.cs which will be invoked on click of Delete Data.

We have used UploadStringAsync of WebClient. The headers content-type of WebClient needs to be set as "application/json" and Encoding as Encoding.UTF8.

UploadStringAsync should have "DELETE" which is HTTP method.
void btnDeleteData_Click(object sender, RoutedEventArgs e)
{
   try
   {
      Customer customer = new Customer();
      customer.CustomerId = 1;

      string jsonData = JsonConvert.SerializeObject(customer);
      WebClient webClient = new WebClient();
      Uri uri = new Uri("http://localhost:1712/api/customer/", UriKind.Absolute);

      webClient = new WebClient();
      webClient.Headers["Content-type"] = "application/json";
      webClient.Encoding = Encoding.UTF8;

      webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
      webClient.UploadStringAsync(uri, "DELETE", jsonData); 
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Step 10: Place btnSaveData_Click method in MainPge.Xaml.cs which will be invoked on click of Save Data.

void btnSaveData_Click(object sender, RoutedEventArgs e)
{
   try
   {
      Customer customer = new Customer();
      customer.CustomerId = 10;   
      customer.CustomerName = txtName.Text;
      customer.Address = txtAddress.Text;

      string jsonData = JsonConvert.SerializeObject(customer);

      WebClient webClient = new WebClient();
      webClient.Headers["Content-type"] = "application/json";
      webClient.Encoding = Encoding.UTF8;

      Uri uri = new Uri("http://localhost:1712/api/customer/", UriKind.Absolute);
      webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
      webClient.UploadStringAsync(uri, "POST", jsonData);
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Step 11: Place below code which will be triggered on completion of upload.

 

void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
   try
   {
      Customer customer = JsonConvert.DeserializeObject<Customer>(e.Result);
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Step 12: Now run Web API application. Put the URI of Web API in the URI of Windows Phone application and run it.

Windows Phone - Web API Save Update Delete

This ends the article of Save, Update and Delete using Web API in Windows Phone

 

Published at DZone with permission of Sumit Dutta, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)