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

Using Using Using Using Using Using Using Using Using Namespace Public Partial Class New Int Public Void Try New

This document contains the code for a C# Windows Forms application that allows users to view, add, edit, and delete user records from a MySQL database table called "info" using a data grid view control. The form initializes connections to the database and loads initial data. It has buttons and event handlers to handle adding, editing, deleting, updating, searching, and retrieving records from the database table to populate the data grid view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Using Using Using Using Using Using Using Using Using Namespace Public Partial Class New Int Public Void Try New

This document contains the code for a C# Windows Forms application that allows users to view, add, edit, and delete user records from a MySQL database table called "info" using a data grid view control. The form initializes connections to the database and loads initial data. It has buttons and event handlers to handle adding, editing, deleting, updating, searching, and retrieving records from the database table to populate the data grid view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

using

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
MySql.Data.MySqlClient;

namespace MyData
{
public partial class Form1 : Form
{
Connection con = new Connection();
MySqlCommand command;
MySqlDataReader reader;
int id;
public void retrieve()
{
try
{
con.conn.Open();
command = new MySqlCommand("Select * from info", con.conn);
reader = command.ExecuteReader();
dataGridViewUser.Rows.Clear();
while (reader.Read())
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridViewUser);
row.Cells[0].Value = reader.GetString("idno");
row.Cells[1].Value = reader.GetString("firstname");
row.Cells[2].Value = reader.GetString("middlename");
row.Cells[3].Value = reader.GetString("lastname");
row.Cells[4].Value = reader.GetString("gender");
row.Cells[5].Value = reader.GetString("birthdate");
row.Cells[6].Value = reader.GetString("address");
dataGridViewUser.Rows.Add(row);
}
con.conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message, "System Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
groupBoxUser.Enabled = true;
btnSave.Enabled = true;

btnAdd.Enabled = false;
btnCancel.Enabled = true;
dataGridViewUser.Enabled = false;
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
cbogender.Text = "";
dateTimePicker1.Text = "";
txtaddress.Text = "";
dataGridViewUser.ClearSelection();
btndelete.Visible = false;
btndelete2.Visible = true;
btnedit.Visible = false;
btnedit2.Visible = true;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (txtfname.Text.Trim() != "" && txtmname.Text.Trim() != "" && txtlname.Text.Trim() != ""
&& txtaddress.Text.Trim() != "")
{
try
{
con.conn.Open();
command = new MySqlCommand("INSERT INTO info (firstname, middlename,
lastname, gender, birthdate, address) VALUES ('" + txtfname.Text.Trim() + "','" +
txtmname.Text.Trim() + "','" + txtlname.Text.Trim() + "','" + cbogender.Text.Trim() + "','" +
dateTimePicker1.Text.Trim() + "','" + txtaddress.Text.Trim() + "')", con.conn);
command.ExecuteNonQuery();
con.conn.Close();
MessageBox.Show("Record successfully saved.", "System Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
retrieve();
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
txtaddress.Text = "";
groupBoxUser.Enabled = false;
btnSave.Enabled = false;
btnAdd.Enabled = true;
btnCancel.Enabled = false;
dataGridViewUser.Rows[0].Selected = false;
btndelete.Visible = false;
btndelete2.Visible = true;
btnedit.Visible = false;
btnedit2.Visible = true;
dataGridViewUser.Enabled = true;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning);

}
}
else
{
MessageBox.Show("Some field/s is/are empty.", "System Message",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Form1_Load(object sender, EventArgs e)
{
con.connect();
retrieve();
dataGridViewUser.Rows[0].Selected = false;
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
dateTimePicker1.Text = "";
cbogender.Text = "";
txtaddress.Text = "";
btndelete.Visible = true;
btndelete2.Visible = true;
btnedit.Visible = false;
btnedit2.Visible = true;
}
private void dataGridViewUser_SelectionChanged(object sender, EventArgs e)
{
btndelete.Enabled = true;
btndelete2.Visible = true;
btndelete.Visible = true;
btnedit.Enabled = true;
btnedit2.Visible = false;
btnedit.Visible = true;

foreach (DataGridViewRow row in dataGridViewUser.SelectedRows)


{
txtfname.Text = row.Cells[1].Value.ToString();
txtmname.Text = row.Cells[2].Value.ToString();
txtlname.Text = row.Cells[3].Value.ToString();
cbogender.Text = row.Cells[4].Value.ToString();
dateTimePicker1.Text = row.Cells[5].Value.ToString();
txtaddress.Text = row.Cells[6].Value.ToString();
}

private void btndelete_Click(object sender, EventArgs e)


{
try

con.conn.Open();
foreach (DataGridViewRow row in dataGridViewUser.SelectedRows)
{
command = new MySqlCommand("DELETE from info WHERE idno = '" +
row.Cells[0].Value.ToString() + "'", con.conn);
}
command.ExecuteNonQuery();
con.conn.Close();
MessageBox.Show("Record successfully deleted.", "System Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
retrieve();
dataGridViewUser.Rows[0].Selected = false;
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
cbogender.Text = "";
dateTimePicker1.Text = "";
txtaddress.Text = "";
btndelete.Visible = false;
btndelete2.Visible = true;
btnedit.Visible = false;
btnedit2.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
groupBoxUser.Enabled = false;
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
cbogender.Text = "";
dateTimePicker1.Text = "";
txtaddress.Text = "";
btnSave.Enabled = false;
btnCancel.Enabled = false;
btnAdd.Enabled = true;
dataGridViewUser.ClearSelection();
btnedit.Enabled = false;
btnedit2.Visible = true;
btnedit.Visible = false;
btnupdate.Enabled = false;
btndelete.Enabled = false;
btndelete2.Visible = true;
btndelete.Visible = false;
dataGridViewUser.Enabled = true;
}
private void btnedit_Click(object sender, EventArgs e)

groupBoxUser.Enabled = true;
btnupdate.Enabled = true;
btnAdd.Enabled = false;
btnCancel.Enabled = true;
btndelete.Enabled = false;
btnedit.Enabled = false;
dataGridViewUser.Enabled = false;

}
private void btnupdate_Click(object sender, EventArgs e)
{
if (txtfname.Text.Trim() != "" && txtmname.Text.Trim() != "" && txtlname.Text.Trim() != ""
&& cbogender.Text.Trim() != "" && txtaddress.Text.Trim() != "")
{
try
{
con.conn.Open();
foreach (DataGridViewRow row in dataGridViewUser.SelectedRows)
{
command = new MySqlCommand("UPDATE info SET firstname = '" +
txtfname.Text.Trim() + "', middlename = '" + txtmname.Text.Trim() + "', lastname = '" +
txtlname.Text.Trim() + "', gender = '" + cbogender.Text.Trim() + "', birthdate = '" +
dateTimePicker1.Text.Trim() + "', address = '" + txtaddress.Text.Trim() + "' WHERE idno = '" +
row.Cells[0].Value.ToString() + "'", con.conn);
}
command.ExecuteNonQuery();
con.conn.Close();
MessageBox.Show("Record successfully updated.", "System Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
retrieve();
dataGridViewUser.Rows[0].Selected = false;
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
cbogender.Text = "";
dateTimePicker1.Text = "";
txtaddress.Text = "";
btndelete.Visible = false;
btndelete2.Visible = true;
btnedit.Visible = false;
btnedit2.Visible = true;
btnCancel.Enabled = false;
dataGridViewUser.Enabled = true;
btnAdd.Enabled = true;
btnupdate.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
else
{

MessageBox.Show("Some field/s is/are empty", "System Message",


MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void txtsearch_TextChanged(object sender, EventArgs e)
{
if (txtsearch.Text.Trim() != "")
{
con.conn.Close();
con.conn.Open();
command = new MySqlCommand("SELECT * FROM info WHERE firstname like '%" +
txtsearch.Text.Trim() + "%' OR middlename like '%" + txtsearch.Text.Trim() + "%' OR lastname
like '%" + txtsearch.Text.Trim() + "%' OR gender like '%" + txtsearch.Text.Trim() + "%' OR
birthdate like '%" + txtsearch.Text.Trim() + "%' OR address like '%" + txtsearch.Text.Trim() + "%'
", con.conn);
reader = command.ExecuteReader();
dataGridViewUser.Rows.Clear();
while (reader.Read())
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridViewUser);
row.Cells[0].Value = reader.GetString("idno");
row.Cells[1].Value = reader.GetString("firstname");
row.Cells[2].Value = reader.GetString("middlename");
row.Cells[3].Value = reader.GetString("lastname");
row.Cells[4].Value = reader.GetString("gender");
row.Cells[5].Value = reader.GetString("birthdate");
row.Cells[6].Value = reader.GetString("address");
dataGridViewUser.Rows.Add(row);
}
con.conn.Close();
}
else
{
retrieve();
}
}
private void btndelete2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult x = MessageBox.Show("Are you sure you want to delete this record?",
"Delete Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (x == DialogResult.Yes)
{
con.conn.Close(); //close all existing connection
con.conn.Open(); //Open connection to database
command = new MySqlCommand("DELETE FROM info WHERE idno = '" +
dataGridViewUser.CurrentRow.Cells[0].Value.ToString() + "' ", con.conn);
command.ExecuteNonQuery();

MessageBox.Show("Record successfully deleted.", "System Message",


MessageBoxButtons.OK, MessageBoxIcon.Information);
con.conn.Close();
retrieve();
}
}
private void dataGridViewUser_CellMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
id = Convert.ToInt32(dataGridViewUser.CurrentRow.Cells[0].Value.ToString());
txtfname.Text = dataGridViewUser.CurrentRow.Cells[1].Value.ToString();
txtmname.Text = dataGridViewUser.CurrentRow.Cells[2].Value.ToString();
txtlname.Text = dataGridViewUser.CurrentRow.Cells[3].Value.ToString();
cbogender.Text = dataGridViewUser.CurrentRow.Cells[4].Value.ToString();
dateTimePicker1.Text = dataGridViewUser.CurrentRow.Cells[5].Value.ToString();
txtaddress.Text = dataGridViewUser.CurrentRow.Cells[6].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
if(txtfname.Text.Trim() != "" && txtmname.Text.Trim() != "" && txtlname.Text.Trim() != ""
&& cbogender.SelectedItem.ToString() != "" && txtaddress.Text.Trim()!="")
{
con.conn.Close(); //close all open connection
con.conn.Open(); //Open connection to database
command = new MySqlCommand("UPDATE info SET firstname = '"
+txtfname.Text.Trim()+ "', middlename = '" + txtmname.Text.Trim() + "', lastname = '" +
txtlname.Text.Trim() + "', gender = '" + cbogender.SelectedItem.ToString() + "', birthdate = '" +
dateTimePicker1.Text + "', address = '" + txtaddress.Text.Trim() + "' WHERE idno = '"+ id +"' ",
con.conn);
command.ExecuteNonQuery();
con.conn.Close();
MessageBox.Show("Record successfully updated.", "System Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
retrieve();
//clear all fields
txtfname.Text = "";
txtmname.Text = "";
txtlname.Text = "";
cbogender.SelectedItem = null;
txtaddress.Text = "";
//disable groupbox
groupBoxUser.Enabled = false;

}
}

You might also like