DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Windows Phone: Save, Update and Delete using Web API

Windows Phone: Save, Update and Delete using Web API

Sumit Dutta user avatar by
Sumit Dutta
·
Jul. 05, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
1.92K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Web API Windows Phone

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Sprint Goals: How to Write, Manage, and Achieve
  • What Makes the Architecture of Geo-Distributed Apps Different?
  • Everything You Need to Know About Web Pentesting: A Complete Guide
  • Event Loop in JavaScript

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo