How To Update Data Using Web API In Xamarin.Forms App

In this article, you will learn how to update data of students by using Web API in Xamarin.Form.

Web API

Web API stands for web application programming interface. It is a framework for building HTTP services that can be consumed by a broad range of clients including desktop clients, browsers, mobiles, iPhone and tablets. Programmatically, a Web API project is very similar to ASP.NET MVC since it uses the MVC design with features such as routing, controllers, actions filter models and binders etc. It is a part of the core ASP.NET platform and can be used with MVC and other type of web applications.

MVC

MVC stands for model view controller. It is a software architectural pattern for implementing user interfaces. In MVC, a project is separated into three interconnected parts, i.e., model, view, and controller.

  • Model - Database operations such as fetch data, add data, update data etc.
  • View - User can interact with the system like HTML and XAMARIN.
  • Controller - Contains the business logic and provides communication between model and view.

How to create the new Web API project?

Steps

  1. Open Visual Studio and create a new project. Select Visual C# > Web > in the left side left side and select ASP.NET Web Applications (.NET Framework) template, provide a project name and click OK. 

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  2. Select Empty template and check MVC and Web API checkboxes and click OK.

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  3. Your project is created. You're now in Solution Explorer. Right click on the Models folder and Add a new class and call it "Students".

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  4. Our Students class will have fields. In my case, I add ID, UserName, Password, and ContactNo. You can add other fields whatever you like. Here is my class with public properties. 
    1. {  
    2.     public int ID {  
    3.         get;  
    4.         set;  
    5.     }  
    6.     public string UserName {  
    7.         get;  
    8.         set;  
    9.     }  
    10.     public string Password {  
    11.         get;  
    12.         set;  
    13.     }  
    14.     public string ContactNo {  
    15.         get;  
    16.         set;  
    17.     }  
    18. }  
  1. Add the controller with select the 3rd no of controller to submit the data for new student. This dialogue box will appear you select your class name in a model class and write the db after the name of the project.

    How To Update The Data Of Student By Using Web API In Xamarin.Form
  1. Copy the students/create and paste in the browser. Now build and run the project.

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  2. Now open the URL in a browser. Here you can create Students records. Enter students data and click Create button to save it.

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  3. Add the 5th controller by Right Click on the Controllers in your project and add a Model class. 

    How To Update The Data Of Student By Using Web API In Xamarin.Form

    How To Update The Data Of Student By Using Web API In Xamarin.Form

  4. Copy the api/students1 and paste in the search bar on browser. Build and run the project. 

    How To Update The Data Of Student By Using Web API In Xamarin.Form
     
    10. Open URL in the browser and you will see output like this. 

    How To Update The Data Of Student By Using Web API In Xamarin.Form

How to update the data of students by using Web API?

Steps

  1. Create the new project of Xamarin.Form and write the following code to build the UI. As you can see from this XAML code, I've used a ListView and DataTemplate to bind Label controls to the fields.
    1. <ListView x:Name="LV">  
    2.     <ItemTemplate>  
    3.         <DataTemplate>  
    4.             <ViewCell>  
    5.                 <StackLayout>  
    6.                     <Label Text="{Binding ID}"/>  
    7.                     <Label Text="{Binding UserName}"/>  
    8.                     <Label Text="{Binding Password}"/>  
    9.                     <Label Text="{Binding ContactNo}"/>  
    10.                 </StackLayout>  
    11.             </ViewCell>  
    12.         </DataTemplate>  
    13.     </ItemTemplate>  
    14. </ListView>  
  1. Open the .cs file of the main page and write the code after initialize component. The GetStudents method connects to the WebAPI and gets the list of Students. After that, the resultset is bound to the ListView using LV.ItemsSource property. The student returned var is a collection of students. 
    1. Getstudents();  
    2. }  
    3. public async void Getstudents()   
    4. {  
    5.     var httpClient = new HttpClient();  
    6.     var response = await httpClient.GetStringAsync("http://localhost:61747/api/students1");  
    7.     var student = JsonConvert.DeserializeObject < List < students >> (response);  
    8.     LV.ItemsSource = student;  
    9. }  
  1. Now, add a new class with the name of Signup and write the following code. It adds these fields, ID, UserName, Password, and ContactNo.

    How To Update The Data Of Student By Using Web API In Xamarin.Form

    How To Update The Data Of Student By Using Web API In Xamarin.Form

HTTP Client
It stands for hypertext transfer protocol. HTTP Client makes a request which communicate to the server what action they want performed.

DeserializeObject
it converts the JSON strings into .NET Object and take the data from database.

SerializeObject
it converts the .NET objects into JSON string.

  1. Add another form, a new page with the name of UpdateStudent and add a StackLayout with the following fieds. On this page, we will add new student data.
    1. <StackLayout>  
    2.     <Entry Placeholder="ID" x:Name="Id"/>  
    3.     <Entry Placeholder="UserName" x:Name="username"/>  
    4.     <Entry Placeholder="Password" x:Name="Password"/>  
    5.     <Entry Placeholder="ContactNo" x:Name="Number"/>  
    6.     <Button Text="Updatestudent" x:Name="Updatestudent" Clicked="Updatestudent_Clicked"/>  
    7. </StackLayout>  
  1. Open the cs file of updatestudents and write the following code. This code creates a Signup object, sets its properties after reading from the form, and calls WebAPI's method to push data to the database. 
    1. Signup student = new Signup  
    2. {  
    3.     ID = Convert.ToInt16(Id.Text),  
    4.         UserName = username.Text,  
    5.         Password = Password.Text,  
    6.         ContactNo = Number.Text  
    7. };  
    8. var httpClient = new HttpClient();  
    9. var json = JsonConvert.SerializeObject(student);  
    10. HttpContent httpContent = new StringContent(json);  
    11. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");  
    12. httpClient.PutAsync(String.Format("http://localhost:61747/api/students1/{0}", student.ID), httpContent);  
    13. DisplayAlert("Added""Your data has been Updated""ok");  
    How To Update The Data Of Student By Using Web API In Xamarin.Form
  1. Now, build and run the application. You will see UI with fields. Enter data and click Updatestudent. 

    How To Update The Data Of Student By Using Web API In Xamarin.Form
7.  The new update date looks like the following:  

How To Update The Data Of Student By Using Web API In Xamarin.Form
 
Summary
 
In this step by step tutorial, I showed how to create a Web API project, connect it with a database, add data records using Web API for a Student Registration form.