How to build a login form, using C# and SQL (2024)

I recently picked up a weekly C# course, and, since I find it interesting, I take my time during the weekend to expand on what is discussed there.

For example, this week we were taught how to create a login form:

Now I’m not here to complain about the design, but the code we used is what bugged me. I know that the purpose of the lesson was to teach us how to open other forms and how to use pictureBoxes, not how to actually create a good login form, but it was so.. unrealistic.

As such, I decided to look into how to create a login form which actually logs you in and has somewhat of a database, you know?

Here’s how to do it:

In order to set up a login form, we need to create what is called a SQL database. To create it, you..

There we go! Now our DB is up and running. The only problem is that it is currently empty. We need to add the login info (Username/Password) to the DB. In order to do this, we’ll be creating a table:

How to build a login form, using C# and SQL (1)

After creating the table, you’ll be greeted with this:

Here is where we’ll add the types of data we’re going to store in our table. For this tutorial, we’ll be using:

  • username, type varchar[50]: where the usernames will be stored. The type varchar simply means “a string of characters of variable length”.
  • password, also type varchar[50].

VERY IMPORTANT: For the sake of simplicity, I will not go through secure password keeping in this tutorial. I will be tackling this issue in a later post, which I will link to here. Please do NOT actually store your passwords in plaintext.

How to build a login form, using C# and SQL (2)

After adding the columns, press the Update button to add them to the DB, then press Update Database. After that, right click the Tables folder and Refresh it so that the table now shows up in the DB.

For now, we’ll be adding a few usernames and passwords manually, in order to test logging in. I repeat, do NOT store passwords in plaintext in actual projects! Hash & salt your passwords!

How to build a login form, using C# and SQL (3)

That’s it for setting up the database. Now, let’s get to the fun part! Actually coding the login button!

We’ll need to find the Connection String of the Database so we can connect our Login Form to it. You can find it by right clicking the DB name and pressing properties:

How to build a login form, using C# and SQL (4)

Here’s how we’re making it work:

How to build a login form, using C# and SQL (5)
How to build a login form, using C# and SQL (6)

As you might have noticed, we’re using quite a few new elements here:

  • The SqlConnection creates a new connection to the DB using a Connection String.
  • With the SqlDataAdapter we communicate between the Sql DB and the DataTable.
  • In the DataTable we’re going to place what entries from the DB match what text the user entered in the username/password fields.
  • Finally, using the sda.Fill(dtbl); we’ll add the entries which match the query sent to the DB (=which elements in the table have the username and password the user wrote in the textBoxes) into the newly made DataTable.

After doing these operations, if there’s at least 1 row in the DataTable (meaning there is a match found with the same username and password in the database) we allow the connection.

Now, we can log in. But how about registering? That’s a bit more tricky. We need to establish a connection to the SQL Server, and issue a query which inserts a new row into our table.

But, before that, we should make sure that there is no one with the same username already registered :)

How to build a login form, using C# and SQL (7)

The logic is the same as when checking the login creditentials, but this time we only look for a matching username in our table.

Now that that’s out of the way, let’s look into adding a new row into the DB!

How to build a login form, using C# and SQL (8)

This is a bit complicated. The way it works is:

  1. Write the query in the commString, where @val1 and @val2 are the user/password combination which is going to be added later.
  2. Connect to the database and create a new SqlCommand(this is safer because now we have control over the input so we can sanitize it)
  3. Add the username and password in place of the val1, val2. This way we can avoid people writing SQL code in the textboxes and deleting my database.
  4. Open the connection and execute the query.

That’s it! Now we’ve got an actual working login system, allowing for registration and which does not “forget” the registered accounts after closing the application.

Next time we’ll go through some security measures that can(and should) be added to our login form (hashing + salting passwords, further prevention of SQL injections).

You can find the code of this project here.

If this has been of any help to you, please press the ❤ button!

How to build a login form, using C# and SQL (2024)
Top Articles
Recipes that use Sourdough Discard
Instant Pot Beets Recipe (Step-By-Step Instructions & Photos)
Kostner Wingback Bed
Devon Lannigan Obituary
Weeminuche Smoke Signal
Pnct Terminal Camera
Nyu Paralegal Program
Brendon Tyler Wharton Height
Georgia Vehicle Registration Fees Calculator
Calamity Hallowed Ore
Best Theia Builds (Talent | Skill Order | Pairing + Pets) In Call of Dragons - AllClash
Jcpenney At Home Associate Kiosk
Turning the System On or Off
No Strings Attached 123Movies
Funny Marco Birth Chart
Echo & the Bunnymen - Lips Like Sugar Lyrics
Bcbs Prefix List Phone Numbers
Vermont Craigs List
Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
360 Tabc Answers
Nearest Walgreens Or Cvs Near Me
Tyler Sis University City
Menards Eau Claire Weekly Ad
Adt Residential Sales Representative Salary
Walmart Near South Lake Tahoe Ca
Johnnie Walker Double Black Costco
Wisconsin Volleyball Team Boobs Uncensored
12 Facts About John J. McCloy: The 20th Century’s Most Powerful American?
Cookie Clicker Advanced Method Unblocked
The Collective - Upscale Downtown Milwaukee Hair Salon
Motorcycle Blue Book Value Honda
Gopher Carts Pensacola Beach
Ts Modesto
Eegees Gift Card Balance
Mastering Serpentine Belt Replacement: A Step-by-Step Guide | The Motor Guy
Manuel Pihakis Obituary
Murphy Funeral Home & Florist Inc. Obituaries
The Legacy 3: The Tree of Might – Walkthrough
Family Fare Ad Allendale Mi
Tmka-19829
Mta Bus Forums
Craigslist Free Manhattan
Cygenoth
Lovein Funeral Obits
Anhedönia Last Name Origin
Best Restaurants Minocqua
Dr Mayy Deadrick Paradise Valley
Fairbanks Auto Repair - University Chevron
Vérificateur De Billet Loto-Québec
Gas Buddy Il
5103 Liberty Ave, North Bergen, NJ 07047 - MLS 240018284 - Coldwell Banker
Ty Glass Sentenced
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5902

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.