Login Page Creation in C#

Introduction

Let's we see how to create a login page in C#. Beginners can easily create a login page in C# using the following procedure.

  1. Create a database in SQL Server 2005
  2. Create a C# project
  3. Connect the created database with the created C# project
  4. Generate code
  5. Check the Login page (Testing)

Create a database in SQL Server 2005

Enter your correct local server name, Username and word

Then you can see the following window.

Use the following procedure

Provide the database name as Login.

Now the database has been created successfully. The following steps will show the table creation in SQL Server. Double-click the database. Now you can see the following window and select the table.

Column Creation

Provide a name for the table.

Created Table

Open the table.

Entering contents into the table.

Create a C# project

Open Visual Studio.

Create a new project.

Choose Visual C# Windows application.

Provide the project name.

Place controls onto the form.

Now the project has been created...!

Connect the created database with the created C# project

Provide the correct servername, username and word. The servername, username and word are all the same as for SQL Server 2005.

Choose "Use SQL Server Authentication".

After entering a correct servername, username and word you can choose the database from the list of databases.

Choose the Login Database.

Check the Connection.

Generate Code

Add the namespace: "using System.Data.SqlClient;".

The connection String is necessary for the connection to the database with our project.

Click the server explorer and then click the login database.

Now you can see the connection string in the properties window:

Copy the connection string and paste it into the code window.

Now click F5 to check the database connection.

Now copy the following code and paste it with in the button click method.

if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
{
    // Empty input handling
}
else
{
    using SqlCommand scmd = new("select Username,word from tbl_Check where Username=@Username and word=@word", sc);
    scmd.Parameters.AddWithValue("@Username", textBox1.Text);
    scmd.Parameters.AddWithValue("@word", textBox2.Text);

    using SqlDataReader sdr = scmd.ExecuteReader();

    if (sdr.HasRows)
    {
        MessageBox.Show("Welcome - The Username and word are Correct", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show("Invalid Username or word", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

Check the Login page (Testing)

Provide the correct username and word that is already stored in the tbl_Check table.

Provide the wrong username and word.

Beginners can use this to create a simple Login page.


Similar Articles