C# - Simple Registration And Login Application

In this tutorial we will create a Simple Registration And Login Application using C#. C# syntax is highly expressive, yet it is also simple and easy to learn. C# is very simplified for the beginners. It is a general-purpose language designed to make all things simpler. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. So let's do the coding...

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/. Here's the link for the SQLite Browser http://sqlitebrowser.org/.

Setting up SQLite

SQLite is very carefully tested prior to every release and relevant to use in some way. SQLite is very usable in any environments especially in embedded devices. First all you need to do is to install the components of the SQLIte database, by right clicking in the Main project title in the solution explorer then selecting the Manage NuGet Packages. tut1 Then go to the browse and search sqlite, after that install it and wait until the process is completed. tut2 Next go to the Updates and update the needed framework to make sqlite work properly. tut3

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Rename the form as Login.cs and then write these codes inside your designer file.
  1. namespace Simple_Registration_and_Login_Application
  2. {
  3. partial class Login
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.label1 = new System.Windows.Forms.Label();
  32. this.txt_username = new System.Windows.Forms.TextBox();
  33. this.label2 = new System.Windows.Forms.Label();
  34. this.txt_password = new System.Windows.Forms.TextBox();
  35. this.btn_login = new System.Windows.Forms.Button();
  36. this.button1 = new System.Windows.Forms.Button();
  37. this.SuspendLayout();
  38. //
  39. // label1
  40. //
  41. this.label1.AutoSize = true;
  42. this.label1.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  43. this.label1.Location = new System.Drawing.Point(128, 54);
  44. this.label1.Name = "label1";
  45. this.label1.Size = new System.Drawing.Size(202, 45);
  46. this.label1.TabIndex = 0;
  47. this.label1.Text = "Username";
  48. //
  49. // txt_username
  50. //
  51. this.txt_username.Font = new System.Drawing.Font("Arial Narrow", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  52. this.txt_username.Location = new System.Drawing.Point(138, 123);
  53. this.txt_username.Name = "txt_username";
  54. this.txt_username.Size = new System.Drawing.Size(439, 53);
  55. this.txt_username.TabIndex = 1;
  56. //
  57. // label2
  58. //
  59. this.label2.AutoSize = true;
  60. this.label2.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  61. this.label2.Location = new System.Drawing.Point(128, 237);
  62. this.label2.Name = "label2";
  63. this.label2.Size = new System.Drawing.Size(193, 45);
  64. this.label2.TabIndex = 2;
  65. this.label2.Text = "Password";
  66. //
  67. // txt_password
  68. //
  69. this.txt_password.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  70. this.txt_password.Location = new System.Drawing.Point(138, 304);
  71. this.txt_password.Name = "txt_password";
  72. this.txt_password.Size = new System.Drawing.Size(439, 53);
  73. this.txt_password.TabIndex = 3;
  74. this.txt_password.UseSystemPasswordChar = true;
  75. //
  76. // btn_login
  77. //
  78. this.btn_login.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  79. this.btn_login.Location = new System.Drawing.Point(238, 396);
  80. this.btn_login.Name = "btn_login";
  81. this.btn_login.Size = new System.Drawing.Size(232, 58);
  82. this.btn_login.TabIndex = 4;
  83. this.btn_login.Text = "LOGIN";
  84. this.btn_login.UseVisualStyleBackColor = true;
  85. this.btn_login.Click += new System.EventHandler(this.LoginButton);
  86. //
  87. // button1
  88. //
  89. this.button1.Location = new System.Drawing.Point(13, 460);
  90. this.button1.Name = "button1";
  91. this.button1.Size = new System.Drawing.Size(89, 39);
  92. this.button1.TabIndex = 5;
  93. this.button1.Text = "Register";
  94. this.button1.UseVisualStyleBackColor = true;
  95. this.button1.Click += new System.EventHandler(this.GoToRegister);
  96. //
  97. // Login
  98. //
  99. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  100. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  101. this.ClientSize = new System.Drawing.Size(721, 511);
  102. this.Controls.Add(this.button1);
  103. this.Controls.Add(this.btn_login);
  104. this.Controls.Add(this.txt_password);
  105. this.Controls.Add(this.label2);
  106. this.Controls.Add(this.txt_username);
  107. this.Controls.Add(this.label1);
  108. this.Name = "Login";
  109. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  110. this.Text = "LOGIN";
  111. this.ResumeLayout(false);
  112. this.PerformLayout();
  113.  
  114. }
  115.  
  116. #endregion
  117.  
  118. private System.Windows.Forms.Label label1;
  119. private System.Windows.Forms.TextBox txt_username;
  120. private System.Windows.Forms.Label label2;
  121. private System.Windows.Forms.TextBox txt_password;
  122. private System.Windows.Forms.Button btn_login;
  123. private System.Windows.Forms.Button button1;
  124. }
  125. }
Next create another form then called it as Registration.cs. This is the form for registering an accounts. To do that write these block of codes inside the designer file.
  1. namespace Simple_Registration_and_Login_Application
  2. {
  3. partial class Registration
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.label1 = new System.Windows.Forms.Label();
  32. this.txt_firstname = new System.Windows.Forms.TextBox();
  33. this.label2 = new System.Windows.Forms.Label();
  34. this.txt_lastname = new System.Windows.Forms.TextBox();
  35. this.txt_username = new System.Windows.Forms.TextBox();
  36. this.label3 = new System.Windows.Forms.Label();
  37. this.txt_password = new System.Windows.Forms.TextBox();
  38. this.label4 = new System.Windows.Forms.Label();
  39. this.button1 = new System.Windows.Forms.Button();
  40. this.button2 = new System.Windows.Forms.Button();
  41. this.SuspendLayout();
  42. //
  43. // label1
  44. //
  45. this.label1.AutoSize = true;
  46. this.label1.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  47. this.label1.Location = new System.Drawing.Point(100, 93);
  48. this.label1.Name = "label1";
  49. this.label1.Size = new System.Drawing.Size(139, 32);
  50. this.label1.TabIndex = 0;
  51. this.label1.Text = "Firstname";
  52. //
  53. // txt_firstname
  54. //
  55. this.txt_firstname.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  56. this.txt_firstname.Location = new System.Drawing.Point(256, 90);
  57. this.txt_firstname.Name = "txt_firstname";
  58. this.txt_firstname.Size = new System.Drawing.Size(276, 40);
  59. this.txt_firstname.TabIndex = 1;
  60. //
  61. // label2
  62. //
  63. this.label2.AutoSize = true;
  64. this.label2.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  65. this.label2.Location = new System.Drawing.Point(100, 161);
  66. this.label2.Name = "label2";
  67. this.label2.Size = new System.Drawing.Size(138, 32);
  68. this.label2.TabIndex = 2;
  69. this.label2.Text = "Lastname";
  70. //
  71. // txt_lastname
  72. //
  73. this.txt_lastname.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  74. this.txt_lastname.Location = new System.Drawing.Point(256, 158);
  75. this.txt_lastname.Name = "txt_lastname";
  76. this.txt_lastname.Size = new System.Drawing.Size(276, 40);
  77. this.txt_lastname.TabIndex = 3;
  78. //
  79. // txt_username
  80. //
  81. this.txt_username.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  82. this.txt_username.Location = new System.Drawing.Point(256, 231);
  83. this.txt_username.Name = "txt_username";
  84. this.txt_username.Size = new System.Drawing.Size(276, 40);
  85. this.txt_username.TabIndex = 5;
  86. //
  87. // label3
  88. //
  89. this.label3.AutoSize = true;
  90. this.label3.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  91. this.label3.Location = new System.Drawing.Point(100, 234);
  92. this.label3.Name = "label3";
  93. this.label3.Size = new System.Drawing.Size(143, 32);
  94. this.label3.TabIndex = 4;
  95. this.label3.Text = "Username";
  96. //
  97. // txt_password
  98. //
  99. this.txt_password.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  100. this.txt_password.Location = new System.Drawing.Point(256, 295);
  101. this.txt_password.Name = "txt_password";
  102. this.txt_password.Size = new System.Drawing.Size(276, 40);
  103. this.txt_password.TabIndex = 7;
  104. this.txt_password.UseSystemPasswordChar = true;
  105. //
  106. // label4
  107. //
  108. this.label4.AutoSize = true;
  109. this.label4.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  110. this.label4.Location = new System.Drawing.Point(100, 298);
  111. this.label4.Name = "label4";
  112. this.label4.Size = new System.Drawing.Size(136, 32);
  113. this.label4.TabIndex = 6;
  114. this.label4.Text = "Password";
  115. //
  116. // button1
  117. //
  118. this.button1.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  119. this.button1.Location = new System.Drawing.Point(246, 387);
  120. this.button1.Name = "button1";
  121. this.button1.Size = new System.Drawing.Size(213, 43);
  122. this.button1.TabIndex = 8;
  123. this.button1.Text = "Register";
  124. this.button1.UseVisualStyleBackColor = true;
  125. this.button1.Click += new System.EventHandler(this.Register);
  126. //
  127. // button2
  128. //
  129. this.button2.Location = new System.Drawing.Point(12, 455);
  130. this.button2.Name = "button2";
  131. this.button2.Size = new System.Drawing.Size(85, 34);
  132. this.button2.TabIndex = 9;
  133. this.button2.Text = "Login";
  134. this.button2.UseVisualStyleBackColor = true;
  135. this.button2.Click += new System.EventHandler(this.GoToLogin);
  136. //
  137. // Registration
  138. //
  139. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  140. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  141. this.ClientSize = new System.Drawing.Size(702, 501);
  142. this.Controls.Add(this.button2);
  143. this.Controls.Add(this.button1);
  144. this.Controls.Add(this.txt_password);
  145. this.Controls.Add(this.label4);
  146. this.Controls.Add(this.txt_username);
  147. this.Controls.Add(this.label3);
  148. this.Controls.Add(this.txt_lastname);
  149. this.Controls.Add(this.label2);
  150. this.Controls.Add(this.txt_firstname);
  151. this.Controls.Add(this.label1);
  152. this.Name = "Registration";
  153. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  154. this.Text = "Registration";
  155. this.ResumeLayout(false);
  156. this.PerformLayout();
  157.  
  158. }
  159.  
  160. #endregion
  161.  
  162. private System.Windows.Forms.Label label1;
  163. private System.Windows.Forms.TextBox txt_firstname;
  164. private System.Windows.Forms.Label label2;
  165. private System.Windows.Forms.TextBox txt_lastname;
  166. private System.Windows.Forms.TextBox txt_username;
  167. private System.Windows.Forms.Label label3;
  168. private System.Windows.Forms.TextBox txt_password;
  169. private System.Windows.Forms.Label label4;
  170. private System.Windows.Forms.Button button1;
  171. private System.Windows.Forms.Button button2;
  172. }
  173. }
Lastly, create another form for the home form after logging in called Home.cs. Just write these block of codes inside the designer file.
  1. namespace Simple_Registration_and_Login_Application
  2. {
  3. partial class Home
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.label1 = new System.Windows.Forms.Label();
  32. this.button1 = new System.Windows.Forms.Button();
  33. this.SuspendLayout();
  34. //
  35. // label1
  36. //
  37. this.label1.AutoSize = true;
  38. this.label1.Font = new System.Drawing.Font("Arial", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  39. this.label1.Location = new System.Drawing.Point(10, 70);
  40. this.label1.Name = "label1";
  41. this.label1.Size = new System.Drawing.Size(457, 60);
  42. this.label1.TabIndex = 0;
  43. this.label1.Text = "WELCOME USER";
  44. //
  45. // button1
  46. //
  47. this.button1.Location = new System.Drawing.Point(174, 242);
  48. this.button1.Name = "button1";
  49. this.button1.Size = new System.Drawing.Size(119, 31);
  50. this.button1.TabIndex = 1;
  51. this.button1.Text = "Logout";
  52. this.button1.UseVisualStyleBackColor = true;
  53. this.button1.Click += new System.EventHandler(this.Logout);
  54. //
  55. // Home
  56. //
  57. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  58. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  59. this.ClientSize = new System.Drawing.Size(479, 325);
  60. this.Controls.Add(this.button1);
  61. this.Controls.Add(this.label1);
  62. this.Name = "Home";
  63. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  64. this.Text = "Home";
  65. this.ResumeLayout(false);
  66. this.PerformLayout();
  67.  
  68. }
  69.  
  70. #endregion
  71.  
  72. private System.Windows.Forms.Label label1;
  73. private System.Windows.Forms.Button button1;
  74. }
  75. }
or also you create the layout by dragging the proper tools to the forms.

Creating the Script

We will now create the script to make things work. To do that go to the csharp script called Registration.cs then right click and select view code, this will force you to go to the text editor. Then write these block of codes inside the Class of the form.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SQLite;
  11.  
  12. namespace Simple_Registration_and_Login_Application
  13. {
  14. public partial class Registration : Form
  15. {
  16. SQLiteConnection conn;
  17. String connectString;
  18.  
  19. public Registration()
  20. {
  21. InitializeComponent();
  22. connectString = @"Data Source=" + Application.StartupPath + @"\Database\login.db; version=3";
  23. }
  24.  
  25. private void Register(object sender, EventArgs e) {
  26. using (conn = new SQLiteConnection(connectString)) {
  27. try {
  28. if (txt_username.Text != "" || txt_password.Text != "")
  29. {
  30. SQLiteCommand cmd = new SQLiteCommand();
  31. cmd.CommandText = @"INSERT INTO member (firstname, lastname, username, password) VALUES(@firstname, @lastname, @username, @password)";
  32. cmd.Connection = conn;
  33. cmd.Parameters.Add(new SQLiteParameter("@firstname", txt_firstname.Text));
  34. cmd.Parameters.Add(new SQLiteParameter("@lastname", txt_lastname.Text));
  35. cmd.Parameters.Add(new SQLiteParameter("@username", txt_username.Text));
  36. cmd.Parameters.Add(new SQLiteParameter("@password", txt_password.Text));
  37.  
  38. conn.Open();
  39.  
  40. int i = cmd.ExecuteNonQuery();
  41.  
  42. if (i == 1)
  43. {
  44. MessageBox.Show("Successfully Created!");
  45. txt_firstname.Text = "";
  46. txt_lastname.Text = "";
  47. txt_username.Text = "";
  48. txt_password.Text = "";
  49. }
  50. }
  51. else {
  52. MessageBox.Show("Required Fields!");
  53. }
  54. }
  55. catch (Exception ex) {
  56. MessageBox.Show(ex.Message);
  57. }
  58. }
  59. }
  60.  
  61. private void GoToLogin(object sender, EventArgs e) {
  62. Login login = new Login();
  63. this.Close();
  64. login.Show();
  65. }
  66.  
  67. }
  68. }
Next, go to the other scripts then write the code provided below corresponded to the name of the scripts. Login.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SQLite;
  11.  
  12. namespace Simple_Registration_and_Login_Application
  13. {
  14. public partial class Login : Form
  15. {
  16.  
  17. SQLiteConnection conn;
  18. SQLiteCommand cmd;
  19. String connectString;
  20. SQLiteDataReader rd;
  21. public Login()
  22. {
  23. InitializeComponent();
  24. connectString = @"Data Source=" + Application.StartupPath + @"\Database\login.db;version=3";
  25. }
  26.  
  27.  
  28. private void LoginButton(object sender, EventArgs e) {
  29. if (txt_username.Text == "" || txt_password.Text == "") {
  30. MessageBox.Show("Required Fields!");
  31. }
  32. else {
  33. using (conn = new SQLiteConnection(connectString))
  34. {
  35. try
  36. {
  37. conn.Open();
  38. string query = "SELECT * FROM member WHERE username='" + txt_username.Text + "' AND password='" + txt_password.Text + "'";
  39. cmd = new SQLiteCommand(query, conn);
  40. cmd.ExecuteNonQuery();
  41. rd = cmd.ExecuteReader();
  42.  
  43. int row = 0;
  44.  
  45. while (rd.Read())
  46. {
  47. row++;
  48. }
  49.  
  50. if (row == 1)
  51. {
  52. MessageBox.Show("Successfully Login");
  53. Home home = new Home();
  54. this.Hide();
  55. home.Show();
  56. }
  57. else
  58. {
  59. MessageBox.Show("Invalid username or password!");
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. MessageBox.Show(ex.Message);
  65. }
  66.  
  67. }
  68. }
  69. }
  70.  
  71. private void GoToRegister(object sender, EventArgs e) {
  72. Registration registration = new Registration();
  73. this.Hide();
  74. registration.Show();
  75. }
  76.  
  77. }
  78.  
  79. }
Home.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Simple_Registration_and_Login_Application
  12. {
  13. public partial class Home : Form
  14. {
  15. public Home()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void Logout(object sender, EventArgs e) {
  21. Login login = new Login();
  22. this.Close();
  23. login.Show();
  24. }
  25. }
  26. }
Try to run the application and see if it works. There you go we successfully created a Simple Registration And Login Application using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Comments

Submitted bysharau (not verified)on Sun, 08/01/2021 - 01:01

thanks

Add new comment