Sample Project
Sample Project
C# Sample Project
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
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100; Type this code
progressBar1.Value = 0;
}
Label
Textbox
Label
Textbox
Button Button
Login Page Code
Code Code
{
string username = txtusername.Text;
string password = txtpassword.Text;
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
Code
Code
{
Student obj = new Student();
obj.Show(); Type this code here
this.Hide();
}
Move to Update/Delete Student Details Form Code
{
UPDATESTUDENT obj = new UPDATESTUDENT();
obj.Show(); Type this code here
this.Hide();
{
Viewstudent obj = new Viewstudent();
obj.Show(); Type this code here
this.Hide();
{
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
Code Code
Database Connection Code of Student Registration.
using System.Data.SqlClient;
}
Reset Button Code
private void btnreset_Click(object sender, EventArgs e)
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
string query = "UPDATE STUDENT SET FIRSTNAME = @FirstName, LASTNAME = @LastName, DATEOFBIRTH = @DateOfBirth, " + "GENDER
= @Gender, PHONENUMBER = @PhoneNumber, ADDRESS = @Address WHERE STUDENTID = @StudentId";
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)
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";
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
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";
}
}
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
6. View Student Form
Label Label
PictureBox
TextBox
DataGridView
Button
Code
Code
View Student Form Database Connection Code
{
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";
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
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT * FROM STUDENT";
dataGridView1.DataSource = dataTable;
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
Double click the view student form and type this code
STEP 5:
STEP 7:
Select “Microsoft SQL Server Database File”
STEP 9:
STEP 11:
STEP 13:
Then click “Add New Table”
1.
2.
Thank you,
Chandrasekaran Harishan