0% found this document useful (0 votes)
12 views24 pages

C# Login and Register with MySQL

Uploaded by

aksumgere
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views24 pages

C# Login and Register with MySQL

Uploaded by

aksumgere
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

 Tutorials

 Downloads

C#, JAVA,PHP,
Programming ,Source Code
▶ Download Projects Source Code
Download now >>

 PROJECTS

 C#

 JAVA

 PHP

 [Link]

 JAVASCRIPT

 DOWNLOADS
Search... ?

➜ Download 10 Java Projects Code ➜ Download 7 C# Projects Code ➜ Download 7 [Link] Projects
Code ➜ Download 5 PHP Projects Code
C# Login And Register Form With MySQL
c# C# Login And Register Form With MySQL c# login form c# signup form Design
Login Form Design Register Form How To Make Login And Signup Form In
C# login register

How to Create Login and Register Form in C#


with MySQL Database

In This C# Tutorial We Will See How To Design a Login & Signup Form and Connect Those
Two Forms With MySQL Database To Allow The Users To Create Their Account or To Access
The Application Using C# Windows Form and Visual Studio Editor .

What We Are Gonna Use In This Project:


- Cshap Programming Language.
- Visual Studio Editor.
- xampp server.
- MySQL Database.
- PhpMyAdmin.
- [Link]( to create the images )

What We Will Do In This Project:


- Design Two Forms For The Login and Register Using Panels, TextBoxes and Labels.
- Create Label To Close The Application On Click.
- Create a Class For The Connection With MySQL Database.
- Connect C# To MySQL Database, To Add The Created User Data In The Signup Form.
- Check If The User Leave Some Fields Empty or Filled With The PlaceHolder Data.
- Check if The Username Already Exists.

- Check If The User Enter a Wrong Password In The Confirmation Field.

WATCH THIS C# TUTORIAL

Project Source Code:

// ------ First We Need To Create a Class To Connect Our Application With The MySQL
Database
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Csharp_Login_And_Register
{
/*
* we need to download the mysql connector
* add the connector to our project
* ( watch the video to see how )
* create a connection now with mysql
* open xampp and start mysql & apache
* go to phpmyadmin and create the users database
*/

class DB
{
// the connection
private MySqlConnection connection = new
MySqlConnection("server=localhost;port=3306;username=root;password=;database=csharp_
users_db");

// create a function to open the connection


public void openConnection()
{
if([Link] == [Link])
{
[Link]();
}
}

// create a function to close the connection


public void closeConnection()
{
if ([Link] == [Link])
{
[Link]();
}
}

// create a function to return the connection


public MySqlConnection getConnection()
{
return connection;
}
}
}

// ------ After Creating The Connection Class, Now We Need To Build a Register Form To
Allow Users To Create Their Account

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Csharp_Login_And_Register
{
public partial class RegisterForm : Form
{
public RegisterForm()
{
InitializeComponent();
}

private void RegisterForm_Load(object sender, EventArgs e)


{
// remove the focus from the textboxes by making a label the active control
[Link] = label1;
}

// textbox first name ENTER


private void textBoxFirstname_Enter(object sender, EventArgs e)
{
String fname = [Link];
if([Link]().Trim().Equals("first name"))
{
[Link] = "";
[Link] = [Link];
}
}

// textbox first name LEAVE


private void textBoxFirstname_Leave(object sender, EventArgs e)
{
String fname = [Link];
if ([Link]().Trim().Equals("first name") || [Link]().Equals(""))
{
[Link] = "first name";
[Link] = [Link];
}
}

// textbox last name ENTER


private void textBoxLastname_Enter(object sender, EventArgs e)
{
String lname = [Link];
if ([Link]().Trim().Equals("last name"))
{
[Link] = "";
[Link] = [Link];
}
}
// textbox last name LEAVE
private void textBoxLastname_Leave(object sender, EventArgs e)
{
String lname = [Link];
if ([Link]().Trim().Equals("last name") || [Link]().Equals(""))
{
[Link] = "last name";
[Link] = [Link];
}
}

// textbox email ENTER


private void textBoxEmail_Enter(object sender, EventArgs e)
{
String email = [Link];
if ([Link]().Trim().Equals("email address"))
{
[Link] = "";
[Link] = [Link];
}
}

// textbox email LEAVE


private void textBoxEmail_Leave(object sender, EventArgs e)
{
String email = [Link];
if ([Link]().Trim().Equals("email address") || [Link]().Equals(""))
{
[Link] = "email address";
[Link] = [Link];
}
}

// textbox username ENTER


private void textBoxUsername_Enter(object sender, EventArgs e)
{
String username = [Link];
if ([Link]().Trim().Equals("username"))
{
[Link] = "";
[Link] = [Link];
}
}

// textbox username LEAVE


private void textBoxUsername_Leave(object sender, EventArgs e)
{
String username = [Link];
if ([Link]().Trim().Equals("username") || [Link]().Equals(""))
{
[Link] = "username";
[Link] = [Link];
}
}

// textbox password ENTER


private void textBoxPassword_Enter(object sender, EventArgs e)
{
String password = [Link];
if ([Link]().Trim().Equals("password"))
{
[Link] = "";
[Link] = true;
[Link] = [Link];
}
}

// textbox password LEAVE


private void textBoxPassword_Leave(object sender, EventArgs e)
{
String password = [Link];
if ([Link]().Trim().Equals("password") || [Link]().Equals(""))
{
[Link] = "password";
[Link] = false;
[Link] = [Link];
}
}

// textbox confirm password ENTER


private void textBoxPasswordConfirm_Enter(object sender, EventArgs e)
{
String cpassword = [Link];
if ([Link]().Trim().Equals("confirm password"))
{
[Link] = "";
[Link] = true;
[Link] = [Link];
}
}

// textbox confirm password LEAVE


private void textBoxPasswordConfirm_Leave(object sender, EventArgs e)
{
String cpassword = [Link];
if ([Link]().Trim().Equals("confirm password") ||
[Link]().Trim().Equals("password") ||
[Link]().Equals(""))
{
[Link] = "confirm password";
[Link] = false;
[Link] = [Link];
}
}

// label close CLICK


private void labelClose_Click(object sender, EventArgs e)
{
//[Link]();
[Link]();
}

// label close MOUSE ENTER


private void labelClose_MouseEnter(object sender, EventArgs e)
{
[Link] = [Link];
}

// label close MOUSE LEAVE


private void labelClose_MouseLeave(object sender, EventArgs e)
{
[Link] = [Link];
}

// button signup
private void buttonCreateAccount_Click(object sender, EventArgs e)
{
// add a new user

DB db = new DB();
MySqlCommand command = new MySqlCommand("INSERT INTO
`users`(`firstname`, `lastname`, `emailaddress`, `username`, `password`) VALUES (@fn, @ln,
@email, @usn, @pass)", [Link]());

[Link]("@fn", [Link]).Value =
[Link];
[Link]("@ln", [Link]).Value =
[Link];
[Link]("@email", [Link]).Value =
[Link];
[Link]("@usn", [Link]).Value =
[Link];
[Link]("@pass", [Link]).Value =
[Link];

// open the connection


[Link]();

// check if the textboxes contains the default values


if (!checkTextBoxesValues())
{
// check if the password equal the confirm password
if([Link]([Link]))
{
// check if this username already exists
if (checkUsername())
{
[Link]("This Username Already Exists, Select A Different
One","Duplicate Username",[Link],[Link]);
}
else
{
// execute the query
if ([Link]() == 1)
{
[Link]("Your Account Has Been Created","Account
Created",[Link],[Link]);
}
else
{
[Link]("ERROR");
}
}
}
else
{
[Link]("Wrong Confirmation Password","Password
Error",[Link],[Link]);
}

}
else
{
[Link]("Enter Your Informations First","Empty
Data",[Link],[Link]);
}

// close the connection


[Link]();

// check if the username already exists


public Boolean checkUsername()
{
DB db = new DB();

String username = [Link];

DataTable table = new DataTable();

MySqlDataAdapter adapter = new MySqlDataAdapter();

MySqlCommand command = new MySqlCommand("SELECT * FROM `users`


WHERE `username` = @usn", [Link]());

[Link]("@usn", [Link]).Value = username;

[Link] = command;

[Link](table);

// check if this username already exists in the database


if ([Link] > 0)
{
return true;
}
else
{
return false;
}

// check if the textboxes contains the default values


public Boolean checkTextBoxesValues()
{
String fname = [Link];
String lname = [Link];
String email = [Link];
String uname = [Link];
String pass = [Link];

if([Link]("first name") || [Link]("last name") ||


[Link]("email address") || [Link]("username")
|| [Link]("password"))
{
return true;
}
else
{
return false;
}

}
// label go to the login form MOUSE ENTER
private void labelGoToLogin_MouseEnter(object sender, EventArgs e)
{
[Link] = [Link];
}

// label go to the login form MOUSE LEAVE


private void labelGoToLogin_MouseLeave(object sender, EventArgs e)
{
[Link] = [Link];
}

// label go to the login form CLICK


private void labelGoToLogin_Click(object sender, EventArgs e)
{
[Link]();
LoginForm loginform = new LoginForm();
[Link]();
}

}
}

// ------ After The User Have Created His Acoount, He Need To Login To The Application, So
Let's Create The Login Form.
using [Link];
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Csharp_Login_And_Register
{
public partial class LoginForm : Form
{

public LoginForm()
{
InitializeComponent();

// set the autosize for the password to false


[Link] = false;

// set the the height of the password to 50


[Link] = new Size([Link], 50);
}

// label close MOUSE ENTER


private void labelClose_MouseEnter(object sender, EventArgs e)
{
[Link] = [Link];
}

// label close MOUSE LEAVE


private void labelClose_MouseLeave(object sender, EventArgs e)
{
[Link] = [Link];
}

// label close CLICK


private void labelClose_Click(object sender, EventArgs e)
{
//[Link]();
[Link]();
}

// button login
private void buttonLogin_Click(object sender, EventArgs e)
{
DB db = new DB();

String username = [Link];


String password = [Link];

DataTable table = new DataTable();

MySqlDataAdapter adapter = new MySqlDataAdapter();

MySqlCommand command = new MySqlCommand("SELECT * FROM `users`


WHERE `username` = @usn and `password` = @pass", [Link]());
[Link]("@usn", [Link]).Value = username;
[Link]("@pass", [Link]).Value = password;

[Link] = command;

[Link](table);

// check if the user exists or not


if ([Link] > 0)
{
[Link]();
MainForm mainform = new MainForm();
[Link]();
}
else
{
// check if the username field is empty
if([Link]().Equals(""))
{
[Link]("Enter Your Username To Login","Empty
Username",[Link],[Link]);
}
// check if the password field is empty
else if ([Link]().Equals(""))
{
[Link]("Enter Your Password To Login", "Empty Password",
[Link], [Link]);
}

// check if the username or the password don't exist


else
{
[Link]("Wrong Username Or Password", "Wrong Data",
[Link], [Link]);
}
}

// label go to signup CLICK


private void labelGoToSignUp_Click(object sender, EventArgs e)
{
[Link]();
RegisterForm registerform = new RegisterForm();
[Link]();
}

// label go to signup MOUSE ENTER


private void labelGoToSignUp_MouseEnter(object sender, EventArgs e)
{
[Link] = [Link];
}

// label go to signup MOUSE LEAVE


private void labelGoToSignUp_MouseLeave(object sender, EventArgs e)
{
[Link] = [Link];
}
}
}
OUTPUT:
Register Form

Error Message If The Fields Are Empty


Error Message If The User Enter a Different Password In The Confirmation

You might also like