Professional Documents
Culture Documents
Shree Nrisingh Madhyamik Vidhyalaya
Shree Nrisingh Madhyamik Vidhyalaya
Piparamath,Birgunj-14
Roll No. : 7
Reg No. : 792340210007
RECOMMENDATION
…………………
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
………………… ……………………………………
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
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.
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;
}
}
}
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.
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.
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" );
}
register.Show(); 15
}
private void button3_Click(object
sender, EventArgs e)
{
// closes the application
Environment.Exit(0);
}
}
}
16
On click of the exit button, add the following code.
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