Register, Login and Update using SQLite in Xamarin.Forms
Tags: sqlite, xamarin, xamarinforms
Published: 2019-11-13 17:24:39

---
title: Register, Login and Update using SQLite in Xamarin.Forms
published: true
date: 2019-06-12 17:57:00 UTC
tags: SQLite,Xamarin,Xamarin.Forms
canonical_url: https://logeshpalani.blogspot.com/2019/06/register-login-and-update-using-sqlite.html
---

## Introduction

  
                              
 This article demonstrates how to register, login, and update the user data in SQLite. We perform CRUD operations with SQLite DB in Xamarin.Forms and can easily store and retrieve objects from the local database.

  

### Output:

  

[![](https://1.bp.blogspot.com/-OTghAkJekjc/XQEvZBHKG6I/AAAAAAAAOJc/n_nXQGOK-PkAgDohThfeq0uoDMWzlmCWACLcBGAs/s640/Screenshot%2B%252820%2529.png)](https://1.bp.blogspot.com/-OTghAkJekjc/XQEvZBHKG6I/AAAAAAAAOJc/n_nXQGOK-PkAgDohThfeq0uoDMWzlmCWACLcBGAs/s1600/Screenshot%2B%252820%2529.png)

  

## Prerequisites

- Visual Studio 2019
- This sample project is targeted only for Android and iOS.

## Step 1:

You can create a new Xamarin.Forms application by going to File \>\> New and choose C# language. Then, select Xamarin.Forms App and click Create. In the next window, enter your application and solution name and then click the Create button to create the project.

(Eg: Project name —_ SqliteLoginDemo_)

[![](https://1.bp.blogspot.com/-DzfpRMMsR0U/XQEvsuFyifI/AAAAAAAAOJk/Q6tjBHtjGTQvql03in2vKR_-MgYqiILfACLcBGAs/s400/Screenshot%2B%252813%2529.png)](https://1.bp.blogspot.com/-DzfpRMMsR0U/XQEvsuFyifI/AAAAAAAAOJk/Q6tjBHtjGTQvql03in2vKR_-MgYqiILfACLcBGAs/s1600/Screenshot%2B%252813%2529.png)

  

## Step 2:

After the project creation, add the SQLite Nuget Package to your project.

- Package Name: _SQLite.Net-PCL_

For that, open the Solution Explorer and select your Solution. Right-click and select _"Manage NuGet Packages for the Solution."_ In the popup window, click navigate to the "_Browse_" tab and click browse "SQLite.Net-PCL" and select the following NuGet packages and select your project, then install it.

## Step 3:

Now, add Interface named ISQLiteInterface in Shared Project. For that, go to Solution Explorer and right click to select Add \>\> Class and followed by select Interface and give name as ISQLiteInterface. and double click to open the interface class and this interface code is given below. One method is called ISQLiteConnection, which will return the database path connection from native applications.
<script src="https://gist.github.com/logeshpalani98/19e0acf45184381673e6f75ab6a5f16b.js"></script>  

## Step 4:

Next, we are going to create a SQLite DB path in Android and iOS. For that, expand SqliteLoginDemo.Android project and create a class named _ISQLiteDbInterface\_Android.cs_ and also follow the same steps for iOS to create an interface named _ISQLiteDbInterface\_iOS.cs_. The platform-specific code is given below. In the class, we add metadata code above the namespace because this class is derived for Dependency Service.

_Android: ISQLiteDbInterface\_Android.cs_
<script src="https://gist.github.com/logeshpalani98/cc8c2d00dfabd8463799d1c22c9965db.js"></script>  
_IOS : __ISQLiteDbInterface\_iOS.cs_  
  
<script src="https://gist.github.com/logeshpalani98/d49c89b5aed5618d162403571f404b21.js"></script>  

## Step 5:

Now, go to 'create a model' from the shared project. For that, go to Solution Explorer \>\> SQliteDemo(Shared) \>\> Right click and select Add \>\> class and give the class name and click Create. The class contains five fields, the first field is Primary key and AutoIncrement. The other fields are username, password, and phone number.  
  
_User.cs_
  
<script src="https://gist.github.com/logeshpalani98/8bb3057d2c24644751b1f89edab84fff.js"></script>  

## Step 6:

After that, create another Helper class named UserDB.cs. In this class, we are creating a new private SQLiteConnection. The Interface return DB path is stored in this SQLiteConnection. The queries are written based on LINQ methods. There is no need to write SQL queries. The GetUser method will return all users from the database. The GetSpecificUser method passes an id parameter to return that specific user. The AddUser method will add the new user. If the user id already exists, it will return 'already exists.' The updateUser method is used for updating the existing user data like a forgotten user password. The deleteUser method is used to delete the specific user. The last one is LoginValidate  
method, which is used to check if the user exists or not.  
  
_UserDB.cs_
<script src="https://gist.github.com/logeshpalani98/6a2ff062a68326185268229ec78d9b02.js"></script>  

## Step 7:

Finally, go to the UI design of the application. The app pages are Main Page, Login Page, Sign Up Page, Home Page. First, design the MainPage of the app. This page contains three visual elements.

1 — Image: This is the logo of the app

2 — Button: Login

3 — Button: Sign Up

This page code is given below.
_MainPage.xaml_  
<script src="https://gist.github.com/logeshpalani98/339f624868d5229781cb71cb7e068e98.js"></script>  

### Step 7.1:

Now, open the code behind the main page. Add the following code. Here, we implemented a two-click event to navigate to another page.  
_MainPage.xaml.cs_

<script src="https://gist.github.com/logeshpalani98/8d0cc27606b382deb7c555d464e84006.js"></script>  

## Step 8:

After that, create another ContentPage named RegisterPage. This page contains different visual elements for registering or adding new user data to the database, and this design page consumes some user data [validation behavior](https://www.c-sharpcorner.com/article/input-validation-in-xamarin-forms-behaviors/) like email validation and password validation.

1. 
Entry — Email Id
2. 
Entry — User name
3. 
Entry — Password
4. 
Entry — Confirm Password
5. 
Entry — Mobile number
6. 
Button — Sign up
7. 
Button — Login: If you already exist, go to Login Page 
_RegistrationPage.xaml_  
  
  
<script src="https://gist.github.com/logeshpalani98/a161cf772e845d744f8eca70b69b9cda.js"></script>  

### Step 8.1:

Afterward, open the code behind RegisterPage. This class code is given below. In this page code, the logic is to check the database for whether the user is already registered or if you need to add a new user. If the user clicked the signup button, first check that the entered data is valid or invalid and then check the database for if the user is already registered or not. If the user is not registered, add the new user data, otherwise, it will return that user already registered.

_RegistrationPage.xaml.cs_
<script src="https://gist.github.com/logeshpalani98/6a2ff062a68326185268229ec78d9b02.js"></script>  

## Step 9:

Next, create a new ContentPage named LoginPage. The LoginPage design contains different visual elements.

1. 
Entry: Mail id
2. 
Entry: Password
3. 
Button: Validate entered data in the above field.
4. 
Button: Forget Password

_LoginPage.xaml_
<script src="https://gist.github.com/logeshpalani98/5aafc588a4662cc154a07b4b92b5a464.js"></script>  

### Step 9.1:

Now, open the code behind LoginPage, and this class implemented different event functions. The login event implementation checks whether the user Credentials are correct or not. If the user exists, go to the Home Page.

_LoginPage.xaml.cs_
  
<script src="https://gist.github.com/logeshpalani98/5c89fe23b79f088af4dc03dc0c37cd48.js"></script>  

## Step 10:

In this step, create a new ContentPage named homepage. In this page, we designed a simple ListView to add the registered user data from the database, and this page code is given below.

_HomePage.xaml_
<script src="https://gist.github.com/logeshpalani98/ddd5f240cac8065d9efa9a2e0e7487b6.js"></script>  

### Step 10.1:

After that, open the code behind the home page. Here, we bind the user data from the database.

_HomePage.xaml.cs_   
  
  
<script src="https://gist.github.com/logeshpalani98/d1dec689e0bf54cb52b489d5b1fd6c24.js"></script>  

### Output:

[![](https://1.bp.blogspot.com/-OTghAkJekjc/XQEvZBHKG6I/AAAAAAAAOJg/QEijEC9-sIEIRlwiNo6HcQ0pvP56PWr-wCEwYBhgL/s640/Screenshot%2B%252820%2529.png)](https://1.bp.blogspot.com/-OTghAkJekjc/XQEvZBHKG6I/AAAAAAAAOJg/QEijEC9-sIEIRlwiNo6HcQ0pvP56PWr-wCEwYBhgL/s1600/Screenshot%2B%252820%2529.png)
  

## Conclusion
  

I hope you learned how to perform a CRUD operation using Xamarin.Forms. Find the full source code on [GitHub](https://github.com/logeshpalani98/Xamarin.Forms_Login_and_Sign_Up_using_SQLite_DB.git). Thank you, and if you have any suggestions to improve my article, please comment.