0% found this document useful (0 votes)
21 views31 pages

Sample Project

This document provides a comprehensive guide for creating a Student Management System using C#. It includes detailed instructions on designing forms, writing code for functionalities like loading, login, student registration, updating, and viewing student details, along with database connection setup. The aim is to simplify software engineering concepts for beginners and assist them in practical implementation.

Uploaded by

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

Sample Project

This document provides a comprehensive guide for creating a Student Management System using C#. It includes detailed instructions on designing forms, writing code for functionalities like loading, login, student registration, updating, and viewing student details, along with database connection setup. The aim is to simplify software engineering concepts for beginners and assist them in practical implementation.

Uploaded by

pjtdfg4c95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Let’s Make Software Engineering Simple

Esoft Wennappuwa & Chilaw

C# Sample Project

Student Management System

Forms
1. Loading
2. Login
3. Home
4. Student Registration
5. Student Update/Delete
6. View Student Details

Here We Go…….
1. Loading Design

Label

Progress bar
Loading Code

public partial class Loading : Form


{
Timer timer; Type this code
public Loading()
{
InitializeComponent();
InitializeTimer(); Type this code
}

private void InitializeTimer()


{
timer = new Timer(); Type this code
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}

private void Loading_Load(object sender, EventArgs e)

{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100; Type this code
progressBar1.Value = 0;
}

private void timer_Tick(object sender, EventArgs e)


{

if (progressBar1.Value < progressBar1.Maximum)


{
progressBar1.Value += 2;
}
else
{
timer.Stop(); Type this code
this.Hide();

Login loginform = new Login();


loginform.Show();
}
}
2. Login Page Design

Label

Label Picture box

Textbox
Label

Textbox

Button Button
Login Page Code

Double click this button Double click this button


and type the code and type the code

Code Code

Login Button Code


private void btnlogin_Click(object sender, EventArgs e)

{
string username = txtusername.Text;
string password = txtpassword.Text;

if (username == "Harishan" && password == "@123")


{
MessageBox.Show("Login Success");
this.Hide();
home obj = new home();
obj.Show(); Type this code
}

else
{
MessageBox.Show("Invalid Username/Password");

}
Exit Button Code
private void btnexit_Click(object sender, EventArgs e)

{
Application.Exit(); Type this code

3. Home
Label

Button
Picture Box

Button
Picture Box

Button

Picture Box

Button
Home Code

Double click this button


and type the code

Code

Double click this button


and type the code

Code

Double click this button


and type the code Double click this button
and type the code
Code
Code

Move to Student Registration Form Code

private void btnstdregform_Click(object sender, EventArgs e)

{
Student obj = new Student();
obj.Show(); Type this code here
this.Hide();

}
Move to Update/Delete Student Details Form Code

private void btnupdatedelete_Click(object sender, EventArgs e)

{
UPDATESTUDENT obj = new UPDATESTUDENT();
obj.Show(); Type this code here
this.Hide();

Move to View Student Details Form Code

private void btnviewstudent_Click(object sender, EventArgs e)

{
Viewstudent obj = new Viewstudent();
obj.Show(); Type this code here
this.Hide();

Back Button Code

private void btnback_Click(object sender, EventArgs e)

{
Login obj = new Login();
obj.Show(); Type this code here
this.Hide();

}
4. Student Registration Form

Label

Picture Box

Label Textbox
Label Textbox
Label DateTimePicker
Label ComboBox
Label Textbox
Label Textbox
Label Textbox

Button Button Button


Student Registration Form Code

Double click this button Double click this button


and type the code and type the code

Code Code
Database Connection Code of Student Registration.

Type this code here

You must add your connection string here, to know

how to get your connection string - click here

using System.Data.SqlClient;

private string connectionString = @" ";


Register Button Code
Type this code here
private void btnregister_Click(object sender, EventArgs e)

string firstName = firstNameTextBox.Text;


string lastName = lastNameTextBox.Text;
DateTime dateOfBirth = dateOfBirthPicker.Value;
string gender = genderComboBox.SelectedItem.ToString();
int phoneNumber = int.Parse(phoneNumberTextBox.Text);
string address = addressTextBox.Text;
string studentId = studentIdTextBox.Text;

// SQL query to insert data


string query = "INSERT INTO STUDENT (FIRSTNAME, LASTNAME, DATEOFBIRTH,
GENDER, PHONENUMBER, ADDRESS, STUDENTID) " +
"VALUES (@FirstName, @LastName, @DateOfBirth, @Gender,
@PhoneNumber, @Address, @StudentId)";

using (SqlConnection connection = new SqlConnection(connectionString))


{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add parameters to avoid SQL injection
command.Parameters.AddWithValue("@FirstName", firstName);
command.Parameters.AddWithValue("@LastName", lastName);
command.Parameters.AddWithValue("@DateOfBirth", dateOfBirth);
command.Parameters.AddWithValue("@Gender", gender);
command.Parameters.AddWithValue("@PhoneNumber", phoneNumber);
command.Parameters.AddWithValue("@Address", address);
command.Parameters.AddWithValue("@StudentId", studentId);

// Execute the query


command.ExecuteNonQuery();
MessageBox.Show("Data saved successfully!");
}
}
catch (SqlException ex)
{
MessageBox.Show("Error"+ex);
}
}

}
Reset Button Code
private void btnreset_Click(object sender, EventArgs e)

{ Type this code here


firstNameTextBox.Text = "";
lastNameTextBox.Text = "";
dateOfBirthPicker.Text = "";
genderComboBox.Text = "";
phoneNumberTextBox.Text = "";
addressTextBox.Text = "";
studentIdTextBox.Text = "";

5. Student Update / Delete Form

Just Copy & Paste The Same Design Of Student Registration Form To The Update / Delete
Form. Because We Need The Same Design Recheck The Values When Updating And
Deleting. You Just Have To Change The Names Of The Buttons According To It’s Function.
Student Update / Delete Form Code

Double click this button Double click this button Double click this textbox
and type the code and type the code and type the code
Code Code Code
Database Connection Code of Student Update / Delete Form

Type this code here

Type this code here

Type this code here


Update Button Code
Type this code
private void btnupdate_Click(object sender, EventArgs e)

string firstName = firstNameTextBox.Text;


string lastName = lastNameTextBox.Text;
DateTime dateOfBirth = dateOfBirthPicker.Value;
string gender = genderComboBox.SelectedItem.ToString();
int phoneNumber = int.Parse(phoneNumberTextBox.Text);
string address = addressTextBox.Text;
string studentId = studentIdTextBox.Text;

string query = "UPDATE STUDENT SET FIRSTNAME = @FirstName, LASTNAME = @LastName, DATEOFBIRTH = @DateOfBirth, " + "GENDER
= @Gender, PHONENUMBER = @PhoneNumber, ADDRESS = @Address WHERE STUDENTID = @StudentId";

using (SqlConnection connection = new SqlConnection(connectionString))


{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add parameters to avoid SQL injection
command.Parameters.AddWithValue("@FirstName", firstName);
command.Parameters.AddWithValue("@LastName", lastName);
command.Parameters.AddWithValue("@DateOfBirth", dateOfBirth);
command.Parameters.AddWithValue("@Gender", gender);
command.Parameters.AddWithValue("@PhoneNumber", phoneNumber);
command.Parameters.AddWithValue("@Address", address);
command.Parameters.AddWithValue("@StudentId", studentId);

// Execute the query


int rowsAffected = command.ExecuteNonQuery();

if (rowsAffected > 0)
{
MessageBox.Show("Data updated successfully!");
}
else
{
MessageBox.Show("Student ID not found.");
}
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}

}
Delete Button Code
Type this code
private void btndelete_Click(object sender, EventArgs e)

string studentId = studentIdTextBox.Text;

if (string.IsNullOrEmpty(studentId))
{
MessageBox.Show("Please enter a valid Student ID.");
return;
}

DialogResult result = MessageBox.Show("Are you sure you want to delete this student?", "Confirm Deletion",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "DELETE FROM STUDENT WHERE STUDENTID = @StudentId";

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@StudentId", studentId);

int rowsAffected = command.ExecuteNonQuery();

if (rowsAffected > 0)
{
MessageBox.Show("Student deleted successfully.");

}
else
{
MessageBox.Show("Student ID not found.");
}
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}

}
StudentID TextBox Code

Type this code


private void studentIdTextBox_TextChanged(object sender, EventArgs e)

string studentId = studentIdTextBox.Text;

if (!string.IsNullOrEmpty(studentId))
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT FIRSTNAME, LASTNAME, DATEOFBIRTH, GENDER, PHONENUMBER, ADDRESS FROM STUDENT WHERE STUDENTID = @StudentId";

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@StudentId", studentId);

using (SqlDataReader reader = command.ExecuteReader())


{
if (reader.Read())
{
firstNameTextBox.Text = reader["FIRSTNAME"].ToString();
lastNameTextBox.Text = reader["LASTNAME"].ToString();
dateOfBirthPicker.Value = Convert.ToDateTime(reader["DATEOFBIRTH"]);
genderComboBox.SelectedItem = reader["GENDER"].ToString();
phoneNumberTextBox.Text = reader["PHONENUMBER"].ToString();
addressTextBox.Text = reader["ADDRESS"].ToString();
}
else
{

}
}
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}

}
6. View Student Form

Label Label

PictureBox
TextBox

DataGridView

Button

View Student Form Code

Double click on the form


and type the code

Code

Double click this textbox


and type the code

Code
View Student Form Database Connection Code

Type this code here

Type this code here

Type this code here


StudentID TextBox Code
Type this code

private void studentIdTextBox_TextChanged(object sender, EventArgs e)

{
string studentId = studentIdTextBox.Text;

if (!string.IsNullOrEmpty(studentId))
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT * FROM STUDENT WHERE STUDENTID = @StudentId";

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@StudentId", studentId);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);

dataGridView1.DataSource = dataTable;
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
else
{
// Optionally clear the DataGridView if the text box is empty
dataGridView1.DataSource = null;
}

}
Type this code in the view student form

private void LoadAllStudents()

{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT * FROM STUDENT";

using (SqlCommand command = new SqlCommand(query, connection))


{
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);

dataGridView1.DataSource = dataTable;
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}

Type this code

Double click the view student form and type this code

private void Viewstudent_Load(object sender, EventArgs e)


{

LoadAllStudents(); Type this code


}
Now Let’s Create the
Database
STEP 1: Go to “PROJECT”
STEP 2:
Click “Add New Data Source”

STEP 3: Select “Database”

Then click “Next”


STEP 4:

Now click “Dataset”

Then click “Next”

STEP 5:

Now click “New Connection”


STEP 6: Click “Change”

STEP 7:
Select “Microsoft SQL Server Database File”

Then Click “OK”


STEP 8: Type Your Database Name Here

Then click “OK”

STEP 9:

Then click “Next”


STEP 10:

Then click “Next”

STEP 11:

Then click “Finish”


STEP 12:
Now Under Database Explorer You Can See the Database That You’ve Created

Double Click Your


Database

STEP 13:
Then click “Add New Table”

Now Double Click


the Folder “Tables”
STEP 14:

Click Update After Creating the Table

Set Primary Key If


needed
Type Your Table Name within
the square brackets [ ]

For example : STUDENT


Now let’s see how to get the connection string

1.

Right click the


Database

Then click properties

2.

Double click this and


copy the connection
string of your database
3.

Paste the connection string here


private string connectionString = @" ";

This documentation is designed with the intention of simplifying software


engineering concepts for beginners. I hope this guide helps students in their
journey to understand and implement a practical project. Feel free to use,
adapt, and expand this project for your learning.

Thank you,
Chandrasekaran Harishan

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy