Xamarin Android: Create Login Using SQLite Database

What is SQLite?

 
SQLite database has methods to create, delete, and execute SQL commands, and perform other common database management tasks. More details.
 
Let’s start,
 
Step 1
 
Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App.
 
Select the Blank App. Give the project name and the project location.
 
 
Step 2
 
Next, go to Solution Explorer-> Project Name-> Resouces->layout. Right-click on Add. A new dialog box will open. Select Android layout, give name for Newuser.axml and add the New Layout Page.
 
 
Step 3
 
Go to Solution Explorer-> Project Name. Right-click on Add. Open new dialog box. Select Activity and give name for RegisterActivity.cs. Now, add the New Activity Class.
 
 
Step 4
 
Next, we need to create the database table, so we create one Data Layer class. Go to Solution Explorer-> Project Name. Right-click to Add. Open a new dialog box. Select Class, give a name for LoginTable.cs and after it, add New Class.
 
 
Step 5
 
Go to Solution Explorer-> Project Name-> References. Right-Click to manage Nuget Packages and open a new dialog box. This dialog box searches SQLite. Install SQLite-Net Packages.
 
 
Step 6
 
Next, Open the Solution Explorer-> Project Name->Resources->Layout->Main.axml. Click Open Design View. Here, create large text, two plain text, and two buttons.
 
 
Step 7
 
Next step is to Open Solution Explorer-> Project Name->Resources->Layout->Newuser.axml, click Open Design View. Here, create large text, two plain texts, and one button. 
 
 
Step 8
 
Next is to open Solution Explorer-> Project Name->LoginTable.cs, click Open CS code Page View and create table columns.
 
 
C# Code
  1. [PrimaryKey, AutoIncrement, Column("_Id")]  
  2.   
  3. public int id { getset; } // AutoIncrement and set primarykey  
  4.   
  5. [MaxLength(25)]  
  6.   
  7. public string username { getset; }  
  8.   
  9. [MaxLength(15)]  
  10.   
  11. public string password { getset; }  
Step 9
 
Now, open Solution Explorer-> Project Name->MainActivity.cs, click Open CS code Page view and add the following namespaces. First, we create a database. So, after OnCreate(), create new method with name of CreateDB().
 
C# Code
  1. public class MainActivity: Activity  
  2. {  
  3.     EditText txtusername;  
  4.     EditText txtPassword;  
  5.     Button btncreate;  
  6.     Button btnsign;  
  7.     protected override void OnCreate(Bundle bundle)  
  8.     {  
  9.         base.OnCreate(bundle);  
  10.         // Set our view from the "main" layout resource  
  11.         SetContentView(Resource.Layout.Main);  
  12.         // Get our button from the layout resource,  
  13.         // and attach an event to it  
  14.         btnsign = FindViewById < Button > (Resource.Id.btnlogin);  
  15.         btncreate = FindViewById < Button > (Resource.Id.btnregister);  
  16.         txtusername = FindViewById < EditText > (Resource.Id.txtusername);  
  17.         txtPassword = FindViewById < EditText > (Resource.Id.txtpwd);  
  18.         btnsign.Click += Btnsign_Click;  
  19.         btncreate.Click += Btncreate_Click;  
  20.         CreateDB();  
  21.     }  
  22.     private void Btncreate_Click(object sender, EventArgs e)  
  23.     {  
  24.         StartActivity(typeof(RegisterActivity));  
  25.     }  
  26.     private void Btnsign_Click(object sender, EventArgs e)  
  27.     {  
  28.         try  
  29.         {  
  30.             string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Call Database  
  31.             var db = new SQLiteConnection(dpPath);  
  32.             var data = db.Table < LoginTable > (); //Call Table  
  33.             var data1 = data.Where(x => x.username == txtusername.Text && x.password == txtPassword.Text).FirstOrDefault(); //Linq Query  
  34.             if (data1 != null)  
  35.             {  
  36.                 Toast.MakeText(this"Login Success", ToastLength.Short).Show();  
  37.             }  
  38.             else  
  39.             {  
  40.                 Toast.MakeText(this"Username or Password invalid", ToastLength.Short).Show();  
  41.             }  
  42.         }  
  43.         catch (Exception ex)  
  44.         {  
  45.             Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();  
  46.         }  
  47.     }  
  48.     public string CreateDB()  
  49.     {  
  50.         var output = "";  
  51.         output += "Creating Databse if it doesnt exists";  
  52.         string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Create New Database  
  53.         var db = new SQLiteConnection(dpPath);  
  54.         output += "\n Database Created....";  
  55.         return output;  
  56.     }  
  57. }  
Here, the database name is “user.db3”.
 
Step 10
 
Next, open Solution Explorer-> Project Name-> RegisterActivity.cs. Click Open CS code Page view, then add the below namespaces. Next, write the following code. 
 
 
C# Code
  1. public class RegisterActivity: Activity   
  2. {  
  3.     EditText txtusername;  
  4.     EditText txtPassword;  
  5.     Button btncreate;  
  6.     protected override void OnCreate(Bundle savedInstanceState)  
  7.     {  
  8.         base.OnCreate(savedInstanceState);  
  9.         SetContentView(Resource.Layout.Newuser);  
  10.         // Create your application here  
  11.         btncreate = FindViewById < Button > (Resource.Id.btn_reg_create);  
  12.         txtusername = FindViewById < EditText > (Resource.Id.txt_reg_username);  
  13.         txtPassword = FindViewById < EditText > (Resource.Id.txt_reg_password);  
  14.         btncreate.Click += Btncreate_Click;  
  15.     }  
  16.     private void Btncreate_Click(object sender, EventArgs e)   
  17.     {  
  18.         try   
  19.         {  
  20.             string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");  
  21.             var db = new SQLiteConnection(dpPath);  
  22.             db.CreateTable < LoginTable > ();  
  23.             LoginTable tbl = new LoginTable();  
  24.             tbl.username = txtusername.Text;  
  25.             tbl.password = txtPassword.Text;  
  26.             db.Insert(tbl);  
  27.             Toast.MakeText(this"Record Added Successfully...,", ToastLength.Short).Show();  
  28.         } catch (Exception ex) {  
  29.             Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();  
  30.         }  
  31.     }  
  32. }   
Step 11
 
Press F5 or build and run the Application. The first image is to register the new user and then, the next image is to log in to the user.
 
 
 
Finally, we successfully created a login using the SQLite database.


Similar Articles