0% found this document useful (0 votes)
2 views

Database-Connection

The document contains a C# Windows Forms application for managing student information using a MySQL database. It includes functionalities for adding, updating, deleting, and displaying student records in a DataGridView. The application establishes a database connection and handles user input through various buttons and text fields to interact with the student data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Database-Connection

The document contains a C# Windows Forms application for managing student information using a MySQL database. It includes functionalities for adding, updating, deleting, and displaying student records in a DataGridView. The application establishes a database connection and handles user input through various buttons and text fields to interact with the student data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Form:

using MySql.Data.MySqlClient;
using System.Data;

namespace Student_Information
{
public partial class Form1 : Form
{
DatabaseConnection dbConn = new DatabaseConnection();
MySqlConnection conn;
MySqlCommand cmd;
MySqlDataReader reader;
MySqlDataAdapter da;
DataTable dt;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
refreshData();
}

private void refreshData()


{
conn = dbConn.connect();
string sql = "SELECT * FROM tbl_studentinfo";
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(reader);
dataGridView1.DataSource = dt;

conn.Close();
}

private void clear()


{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
numericUpDown1.Value = 1;
button1.Enabled = true;
button2.Enabled = false;
textBox1.ReadOnly = false;

refreshData();
}

private void button1_Click(object sender, EventArgs e)


{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Complete Details", "Add to Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
conn = dbConn.connect();
string sql = "INSERT INTO tbl_studentinfo(student_id, last_name, first_name, middle_name,
age) " +
"VALUES('" + textBox1.Text + "', '" + textBox2.Text + "' ,'" + textBox3.Text + "' , '" +
textBox4.Text + "'," + numericUpDown1.Value + ")";
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();

refreshData();
clear();

MessageBox.Show("Add Successful", "Add to Database", MessageBoxButtons.OK,


MessageBoxIcon.Information);

conn.Close();
}
}

private void button2_Click(object sender, EventArgs e)


{
if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Complete Details", "Update to Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
conn = dbConn.connect();
string sql = "UPDATE tbl_studentinfo SET last_name='" + textBox2.Text + "', first_name='" +
textBox3.Text + "', middle_name='" + textBox4.Text + "', age=" + numericUpDown1.Value + " " +
"WHERE student_id='" + textBox1.Text + "'";
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();

refreshData();
clear();

MessageBox.Show("Update Successful", "Update to Database", MessageBoxButtons.OK,


MessageBoxIcon.Information);

conn.Close();
}
}

private void button3_Click(object sender, EventArgs e)


{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No item to edit", "Sample Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else if (dataGridView1.CurrentRow.Selected == false)
{
MessageBox.Show("Select item to edit", "Sample Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);

}
else
{
textBox1.Text = dataGridView1.CurrentRow.Cells["student_id"].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells["last_name"].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells["first_name"].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells["middle_name"].Value.ToString();
numericUpDown1.Value = int.Parse(dataGridView1.CurrentRow.Cells["age"].Value.ToString());
button1.Enabled = false;
button2.Enabled = true;
textBox1.ReadOnly = true;
}
}

private void button4_Click(object sender, EventArgs e)


{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No item to edit", "Sample Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else if (dataGridView1.CurrentRow.Selected == false)
{
MessageBox.Show("Select item to edit", "Sample Database", MessageBoxButtons.OK,
MessageBoxIcon.Information);

}
else
{
conn = dbConn.connect();
string sql = "DELETE FROM tbl_studentinfo WHERE student_id='" +
dataGridView1.CurrentRow.Cells["student_id"].Value.ToString() + "'";
cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();

refreshData();
clear();

MessageBox.Show("Delete Successful", "Delete to Database", MessageBoxButtons.OK,


MessageBoxIcon.Information);

conn.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
clear();
}

private void button6_Click(object sender, EventArgs e)


{
Application.Exit();
}
}
}
DATABASE CONNECTION:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using MySql.Data.MySqlClient;

namespace Student_Information
{
internal class DatabaseConnection
{
public MySqlConnection connect()
{
string host = "localhost";
string username = "root";
string password = "";
string database = "crud_db";

string connectionString =
$"Server={host};Database={database};User={username};Password=\"{password}\";";

try
{
MySqlConnection conn = new MySqlConnection(connectionString);
conn.Open();
Console.WriteLine("Connection successful.");
return conn;
}
catch (MySqlException ex)
{
Console.WriteLine($"Connection error: {ex.Message}");
return null;
}
}
}
}

You might also like