Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

SHREE NRISINGH MADHYAMIK VIDHYALAYA

Piparamath,Birgunj-14

A project work of Visual Programming


Create Login and Register System
with C# Winforms

Submitted By : Alok Chaurasiya

Roll No. : 7
Reg No. : 792340210007
RECOMMENDATION

It is certified that Alok Chaurasiya has carried out the


project work of Subject under my supervision.

I recommend the project work in the partial fulfillment


for the requirement of
Grade – XII of Visual Programming .

…………………
Supervisor
Shree Nrishingh Madhyamik Vidyalaya
Birgunj-14, Parsa
Date:

I
ACKNOWLEDGEMENTS
It is my good fortune to have a very cooperative supervisor.
During the work in progress, his support, helpfulness and
constant encouragement kept me motivated in research work.
He provided invaluable interest, guidance during the course of
work. I have not only learned but also got important suggestions
regarding scientific writing and other related matters. I am very
grateful to him.
I would like to thank all faculty members who have provided
encouragement and suggestions during course of work and
special thanks to lab assistants.
Further, I would also like to extend my gratitude to the principal
Mr. Binod Mehta and our coordinator Mr. Sanjay Saxena for
providing me the entire faculty that was required.
Finally, I would like to express my deep gratitude to the greatest
mentor, my parents. All the thanks are due to them for
everything that I have achieved.

II
SHREE NRISIHNGH MADHYAMIK VIDYALAYA
Pipramath,Birgunj-14
TECHNICAL EDUCATION IN COMPUTER ENGINEERING

We certify that we have read this project work and in our


opinion it is good in scope and quality as the project work in
the partial fulfillment for the requirement of Grade – XII of
Visual Programming .

………………… ……………………………………
Supervisor Head of Department of
Physics Shree Nrisingh
Madhyamik Vidhyalaya
Birgunj-14, Parsa

III
INTRODUCTIO
N C# or C-sharp is an object-oriented
programming language developed by Microsoft.
C# or C-sharp runs on a framework called the .Net
framework.
The language has created many applications like:
• Web applications
• Mobile applications
• Computer games
• Desktop applications
• Database applications
• Virtual reality applications

PREREQUISITE
S To follow this article along it would be
helpful to have the following:
• An editor, in my case, I’ll be using 
Visual Studio.
• Some basic C# and SQL knowledge.
• Knowledge on how to run relational database
management in this case, we will use MySQL.
• Visual Studio knowledge and how to create
projects.

1
Step I: Create a database and
table with required columns

Create database userdata;

Create table command:

CREATE TABLE `user_info` ( `id` INT NOT NULL AUTO_INCREMENT ,


`names` VARCHAR(50) NOT NULL , `username` VARCHAR(20) NOT NULL
, `password` VARCHAR(50) NOT NULL , PRIMARY KEY (`id`)) ;

Step II: Create a project


Create a Visual Studio project by clicking on File -> New ->
Project and then select Visual C#. From the window, choose
Windows Forms App(.Net Framework). Give your application a
name. Then click ok. The project will come with default form
called Form 1.

2
Step III: Create a config class to
execute MySQL queries
Since C# is object-oriented, we will create a
class that will handle the execution of queries
that we will use in our program.

From the Solution Explorer window right-click


and select add -> New Item -> Class. Name the
class Config.cs the click on the button add.

•Add MySQL.Data Library by right-clicking on


solution explorer window, then Manage Nuget
packages, then search for MySQL.Data Library
and install.

•Add the following class to help in the


execution of MySQL queries.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
//add this for MessageBox
using System.Windows.Forms;
// add data function classes
using System.Data;
3
namespace LoginSysten
{
public class Config
{
string ConectionString = "";
public MySqlConnection connection = null;
public string server = "127.0.0.1";
public string user = "root";
public string password = "";// MySQL password

DataSet ds;
DataTable dt;
public string Table =
"user_info"; // initialize db table
public string ConnectionType = "";
string RecordSource = "";
 
DataGridView tempdata;
 
public Config()
{
 
}
 
// function to connect to the
database
public void Connect(string
database_name) 4
{
try
{
 
ConectionString = "SERVER=" + server + ";" +
"DATABASE=" + database_name + ";" + "UID=" +
user + ";" + "PASSWORD=" + password + ";";
 
connection = new
MySqlConnection(ConectionString);
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
 
// Function to execute select
statements
public void ExecuteSql(string
Sql_command)
{
 
nowquiee(Sql_command);
 
}
 
// creates connection to MySQL before
execution
public void nowquiee(string sql_comm)
 
{
try
{

5
MySqlConnection cs = new
MySqlConnection(ConectionString);
cs.Open();
MySqlCommand myc = new
MySqlCommand(sql_comm, cs);
myc.ExecuteNonQuery();
cs.Close();
 
 
}
catch (Exception err)
{
 

MessageBox.Show(err.Message);
}
}
 
// function to execute delete ,
insert and update
public void Execute(string Sql_command)
{
RecordSource = Sql_command;
ConnectionType = Table;
dt = new
DataTable(ConnectionType);
try
{
string command =
RecordSource.ToUpper();
 

6
//======================if sql contains
select==========================================
MySqlDataAdapter da2 = new
MySqlDataAdapter(RecordSource, connection);
 
DataSet tempds = new DataSet();
da2.Fill(tempds,
ConnectionType);
da2.Fill(tempds);
 

//==============================================
========================================
 
 
}
catch (Exception err)
{ MessageBox.Show(err.Message); }
}
 
// function to bring selected results
based on column name and row index
public string Results(int ROW, string
COLUMN_NAME)
{
try
{
return dt.Rows[ROW]
[COLUMN_NAME].ToString();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return "";
 
7
}
}
 
// function to bring selected results based
on column index and row index
public string Results(int ROW, int COLUMN_NAME)
{
try
{
return dt.Rows[ROW]
[COLUMN_NAME].ToString();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return dt.Rows[ROW]
[COLUMN_NAME].ToString();
 
}
}
 
// Execute select statement
public void ExecuteSelect(string
Sql_command)
{
RecordSource = Sql_command;
ConnectionType = Table;
 
dt = new DataTable(ConnectionType);
try
{
string command =

8
RecordSource.ToUpper();
MySqlDataAdapter da = new
MySqlDataAdapter(RecordSource, connection);
ds = new DataSet();
da.Fill(ds, ConnectionType);
da.Fill(dt);
tempdata = new DataGridView();
}
catch (Exception err)
{
MessageBox.Show(err.Message);

}
 
// count Number of rows
after selecy
public int Count()
{
return dt.Rows.Count;
}
}
}

 Now that we have Config.cs, we can


execute any MySQL statement.

9
Step IV: Create register windows form
In Microsoft Visual Studio, create a new project. Choose
project -> Add Windows Form from File submenu in the
left corner, give the form a name Register , and click
Add.
We have two Windows form classes that is Form1.cs and
Register.cs.

Step V: Design login and register


interface
Login form
 Click on Form1.cs in Solution Explorer, and on the form
that displays, add three buttons, two textboxes, and two
labels.

The first button will be the register button, that launch the
Register form. The second button will be the Login button.
When the second button is clicked, it will query the
database with the input entered. The second button will
execute the login MySQL query. The third button will close
the application.
The first textbox will allow the username input for login,
while the second textbox will enable the password’s input.
These two inputs will be passed to the SQL.

10
The labels will indicate the functionality of the two
textboxes.

Register form
 Click on Register.cs in the Solution Explorer and on the form that
displays add two buttons, three textboxes, and three labels.
The first button will be a register button to save data entered, and the
second one will be an exit button that will close the Register form.
The first textbox will allow the input of the names for the user. The
second textbox will allow input of the user’s username. The third
textbox is for entering the password.

11
The labels will or show the functionality of
the three textboxes.
 

Step VI: Login logic


 Initialize the Config file in Form1.cs to
allow us to access the database with ease.
 

Initialize the connection class


Config db = new Config();
public Form1()
{
12
InitializeComponent();
// pass the database you want to
connect to
db.Connect("userdata");
}
 

On click of the register button, add the following code.

//start register window


Register register = new
Register();
register.Show();
 
On click of the login button, add the following
code.
// querry MySQL database for the data passed
from textboxes
db.ExecuteSelect("SELECT * FROM
`user_info` where username='" + textBox1.Text+
"' and password ='" + textBox2.Text+"'");
 
if (db.Count() == 1)
{
MessageBox.Show("Success You
will Login as"+db.Results(0, "names"));
}
else
{
MessageBox.Show("Wrong
username and password combination" );
}
13
On click of the exit button, add the following code.

private void button3_Click(object


sender, EventArgs e)
{
// closes the application
Environment.Exit(0);
}

Final Form1.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 LoginSysten
{
public partial class Form1 : Form
{
 
// Initialize the connection class
Config db = new Config();
public Form1()
{
InitializeComponent();
 

14
// pass the database you want to connect to
db.Connect("userdata");
}
 
private void button1_Click(object sender,
EventArgs e)
{
// querry MySQL database for the data
passed from textboxes
db.ExecuteSelect("SELECT * FROM
`user_info` where username='" + textBox1.Text+ "'
and password ='" + textBox2.Text+"'");
 
if (db.Count() == 1)
{
MessageBox.Show("Success You will
Login as"+db.Results(0, "names"));
}
else
{
MessageBox.Show("Wrong username
and password combination" );
}

private void button2_Click(object sender, EventArgs e)


{
// start register window
Register register = new Register();

register.Show(); 15
}
 
private void button3_Click(object
sender, EventArgs e)
{
 
// closes the application
Environment.Exit(0);
}
}
}

Step VII: Register logic

 Initialize the Config file in


Register.cs to allow us to access the
database with ease.
 
Config db = new Config();
public Register()
{
InitializeComponent();
// pass the database you want to
connect to
db.Connect("userdata");
}

16
On click of the exit button, add the following code.

private void button3_Click(object sender,


EventArgs e)
{
// closses the register window
this.Close();
}

On click of the register button, add the following code to


save the information.

private void button2_Click(object sender,


EventArgs e)
{
// saves data in the database
db.Execute("INSERT INTO `user_info`
(`id`, `names`, `username`, `password`) VALUES
(NULL, '"+textBox3.Text+"', '"+textBox1.Text+"',
'"+textBox2.Text+"');");
 
this.Close()
}

17
Conclusion
From the example above, we have seen how we can use
C# to create a Desktop system with a login functionality.
The object-oriented functionality helps in code re-use
without doing much coding from scratch.
In this article:
 We created a database for our application.
 Created Login and Register Interfaces.
 Connected our application with the database.
 Coded logic and functionality.

18

You might also like