Login Form in VB.NET with SQL

Prerequisites

  • Visual Studio: Ensure you have Visual Studio installed with support for VB.NET and SQL Server.
  • SQL Server: SQL Server should be installed and running. You can use SQL Server Express for a lightweight option.

Step 1: Set Up the SQL Database

  1. Create a New Database

    • Open SQL Server Management Studio (SSMS).
    • Connect to your SQL Server instance.
    • Right-click on Databases and select New Database.
    • Name the database (e.g., UserDatabase) and click OK.
  2. Create a Table for User Credentials

    • Right-click on the newly created database and select New Query.

    • Enter the following SQL script to create a table for storing user credentials:

       
      CREATE TABLE Users ( UserID INT PRIMARY KEY IDENTITY(1,1), Username NVARCHAR(50) NOT NULL UNIQUE, PasswordHash NVARCHAR(256) NOT NULL );
    • Run the script by clicking the Execute button

Insert Sample Data

  • To add a sample user, use the following SQL script:

    INSERT INTO Users (Username, PasswordHash) VALUES ('admin', HASHBYTES('SHA2_256', 'password123'));
  • This script inserts a user with the username admin and a hashed password. Note that HASHBYTES is used for hashing passwords. In a real application, you should use a more secure hashing approach and salt passwords

  1. Add SQL Database Connection

    • In Solution Explorer, right-click on your project and select Add -> New Item -> Class. Name it DatabaseHelper.vb.

    • Add the following code to handle database connections and queries:

      Imports System.Data.SqlClient Public Class DatabaseHelper Public Function ValidateUser(username As String, password As String) As Boolean Dim query As String = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND PasswordHash = HASHBYTES('SHA2_256', @Password)" Using connection As New SqlConnection(ConnectionString) Using command As New SqlCommand(query, connection) command.Parameters.AddWithValue("@Username", username) command.Parameters.AddWithValue("@Password", password) connection.Open() Dim result As Integer = Convert.ToInt32(command.ExecuteScalar()) Return result > 0 End Using End Using End Function End Class
    • Replace YOUR_SERVER_NAME with your SQL Server instance name.

  2. Write the Code for the Login Button

    • Double-click on the Login Button (btnLogin) to open the code editor.

    • Add the following code to handle user login:

       
      Public Class LoginFrom1 Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click Dim username As String = txtUsername.Text Dim password As String = txtPassword.Text If dbHelper.ValidateUser(username, password) Then MessageBox.Show("Login successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show("Invalid username or password. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
    • Private Function ValidateUser(username As String, password As String) As Boolean
    • Dim ConnectionString As String = "Server=YOUR_SERVER_NAME;Database=UserDatabase;Trusted_Connection=True;"
    • Dim query As String = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND PasswordHash = HASHBYTES('SHA2_256', @Password)" Using connection As New SqlConnection(ConnectionString) Using command As New SqlCommand(query, connection) command.Parameters.AddWithValue("@Username", username) command.Parameters.AddWithValue("@Password", password) connection.Open() Dim result As Integer = Convert.ToInt32(command.ExecuteScalar()) Return result > 0 End Using End Using End Function
End Class

Step 3: Test Your Application

  1. Run Your Application
    • Press F5 to run the application.
    • Enter the username and password (e.g., admin and password123) and click the Login button.
    • You should see a success message if the credentials are correct, or an error message if they are not.

Published on Sept. 27, 2024, 6:09 p.m.