Introduction
In this article, we will create a simple login and registration form with a database in a C# Windows Forms application. The app includes a registration page for creating a new account, a login page for signing in, and a home page that shows a success message after login.
Create the Project
- Open Visual Studio. The original article used Visual Studio 2019.
- Go to File, then New, then Project.
- Search for Windows Forms App (.NET Framework) and click Next.
- Enter the project details and click Create.
The project details you need are:
- Project Name: the name of your project
- Location: where the app will be stored on your computer
- Solution Name: the name shown in Solution Explorer
- Framework: choose the framework required by your app
Add the Forms
After the project is created, open Solution Explorer. If it is not visible, use the View menu or press Ctrl+W, S. Then add the forms you need for the application.
Right-click the solution name, choose Add, and then Add New Item. Create the following forms:
- Login form
- Registration form
- Home form
Add the Database
Now add a database to the project. Open the Add New Item dialog again, select the Data category, then choose a service-based database and click Add.
Open the database file in Server Explorer, expand the database, right-click Tables, and choose Add New Table.
Create the fields you need. The original article used Id, UserName, and Password, with Id set as an identity column.
Build the Registration Form
Design the registration form as needed. The article used a basic layout with username, password, confirm password, and register controls.
Configure the Connection String
Click anywhere on the registration form to generate the Form_Load event and add the database connection code there.
Use the connection string from the database connection dialog. The article copied the path from Visual Studio and used it directly in the form load event.
Registration Flow
Use the login button on the registration form to open the login page.
- Hide the current registration form.
- Create a new login form instance.
- Show the login form.
Add the registration button logic next.
- Check that all fields contain values.
- Verify that password and confirm password match.
- Check whether the username already exists.
- Insert the new record into the database.
Build the Login Form
Create the login page with two textboxes for username and password, plus buttons for login and opening the registration form.
Login Flow
Add the same database connection code to the login form load event.
When the registration button is clicked, open the registration form.
When the login button is clicked, validate the credentials and open the home form if the user exists.
The original article checks that both fields are filled, then looks up the matching record in the database. If the account exists, the home form opens.
Update the Start Form
Change the startup form in Program.cs so the application opens the login page first.
Run the Application
After the forms, database, and events are wired up, run the application and test the registration and login flow.



Source Code
Login.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Login : Form
{
SqlCommand cmd;
SqlConnection cn;
SqlDataReader dr;
public Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Articles\Code\RegistrationAndLogin\Database.mdf;Integrated Security=True");
cn.Open();
}
private void Btnregister_Click(object sender, EventArgs e)
{
this.Hide();
Registration registration = new Registration();
registration.ShowDialog();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
if (txtpassword.Text != string.Empty && txtusername.Text != string.Empty)
{
cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", cn);
dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
this.Hide();
Home home = new Home();
home.ShowDialog();
}
else
{
dr.Close();
MessageBox.Show("No account available with this username and password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please enter a value in all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}Registration.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Registration : Form
{
SqlCommand cmd;
SqlConnection cn;
SqlDataReader dr;
public Registration()
{
InitializeComponent();
}
private void Registration_Load(object sender, EventArgs e)
{
cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Articles\Code\RegistrationAndLogin\Database.mdf;Integrated Security=True");
cn.Open();
}
private void BtnRegister_Click(object sender, EventArgs e)
{
if (txtconfirmpassword.Text != string.Empty && txtpassword.Text != string.Empty && txtusername.Text != string.Empty)
{
if (txtpassword.Text == txtconfirmpassword.Text)
{
cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "'", cn);
dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
MessageBox.Show("Username already exists. Please try another.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
dr.Close();
cmd = new SqlCommand("insert into LoginTable values(@username,@password)", cn);
cmd.Parameters.AddWithValue("username", txtusername.Text);
cmd.Parameters.AddWithValue("password", txtpassword.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Your account is created. Please login now.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Please enter the same password in both fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please enter a value in all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Button1_Click(object sender, EventArgs e)
{
this.Hide();
Login login = new Login();
login.ShowDialog();
}
}
}Home.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
}
}Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
}
}Conclusion
This walkthrough shows how to create a basic login and registration page in a Windows Forms application backed by SQL Server LocalDB. It is a simple foundation, but it is useful for understanding form flow, database checks, and record insertion in a desktop app.
