Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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;
using MySql.Data.MySqlClient;

namespace DBCon4
{
public partial class Form1 : Form
{
//for connection
MySqlConnection CN = new MySqlConnection("server=localhost;uid=root;
password=''; database=dbCon4");
MySqlCommand Com = new MySqlCommand();
MySqlDataReader reader;
int numberofitemsperpage = 0;
int itemsshowed = 0,i=0;

public Form1()
{
InitializeComponent();
Com.Connection = CN;
}

private void dt1_ValueChanged(object sender, EventArgs e)


{
// MessageBox.Show(dt1.Value.ToString("yyyy - MM - dd"));

int age = DateTime.Now.Year - dt1.Value.Year;


if (DateTime.Now.Month < dt1.Value.Month || DateTime.Now.Month ==
dt1.Value.Month && DateTime.Now.Day < dt1.Value.Day)
age--;
txtAge.Text = age.ToString();
}

private void Form1_Load(object sender, EventArgs e)


{
loadgrid();
btnEdit.Enabled = false;
btnDelete.Enabled = false;

private void btnAdd_Click(object sender, EventArgs e)


{
CN.Open();
Com.CommandText = "INSERT INTO student(sname, dob, age) VALUES('" +
txtName.Text + "','" + dt1.Text + "', '" + txtAge.Text + "')";
// Com = new MySqlCommand("INSERT INTO user(uname, pwd, uytpe)
VALUES('" + textBox1.Text + "', '" + textBox2.Text + "', '" + comboBox1.Text +
"')", CN);
Com.ExecuteNonQuery();
MessageBox.Show("Record Saved.", "Save", MessageBoxButtons.OK,
MessageBoxIcon.Information);
CN.Close();

loadgrid();
txtName.Clear();
txtAge.Clear();
dt1.Text = "";

private void btnEdit_Click(object sender, EventArgs e)


{
//for updating records
CN.Open();
Com.CommandText = "UPDATE student SET sname='" + txtName.Text + "',
dob='" + dt1.Text + "', age='" + txtAge.Text + "' WHERE sid='" + txtID.Text +
"'";
Com.ExecuteNonQuery();
MessageBox.Show("Record Updated.", "Update", MessageBoxButtons.OK,
MessageBoxIcon.Information);
CN.Close();
loadgrid();
btnAdd.Enabled = true;
btnEdit.Enabled = false;
btnDelete.Enabled = false;
}

private void btnDelete_Click(object sender, EventArgs e)


{
//for deleting record(s)

if (MessageBox.Show("Are you sure?", "CONFIRM DELETE ",


MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
CN.Open();
Com.CommandText = "Delete FROM user WHERE sid='" + txtID.Text
+ "'";
Com.ExecuteNonQuery();
CN.Close();
MessageBox.Show("Record Deleted.", "Delete",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtID.Clear();
txtName.Clear();
loadgrid();
btnAdd.Enabled = true;
btnEdit.Enabled = false;
btnDelete.Enabled = false;

}
void loadgrid()
{
dataGridView1.Rows.Clear();
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Student Name";
dataGridView1.Columns[2].Name = "Date of Birth";
dataGridView1.Columns[3].Name = "Age";
CN.Open();
Com.CommandText = "select * from student order by sname";

reader = Com.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows.Add(reader["sid"].ToString(),
reader["sname"].ToString(), reader["dob"].ToString(),
reader["age"].ToString());
}

reader.Close();
CN.Close();
}
void loadgrid2()
{
dataGridView1.Rows.Clear();
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Student Name";
dataGridView1.Columns[2].Name = "Date of Birth";
dataGridView1.Columns[3].Name = "Age";
CN.Open();
Com.CommandText = "select * from student where dob between '" +
dtStart.Value.ToString("yyyy-MM-dd") + "' AND '" + dtEnd.Value.ToString("yyyy-
MM-dd") + "' order by sname";

reader = Com.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows.Add(reader["sid"].ToString(),
reader["sname"].ToString(), reader["dob"].ToString(),
reader["age"].ToString());
}

reader.Close();
CN.Close();
}

private void dataGridView1_CellMouseDoubleClick(object sender,


DataGridViewCellMouseEventArgs e)
{
btnAdd.Enabled = false;
btnEdit.Enabled = true;
btnDelete.Enabled = true;

//dataGridView1_CellMouseClick
txtID.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
dt1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
txtAge.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}

private void dtEnd_ValueChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
loadgrid2();
}

private void printDocument1_PrintPage(object sender,


System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Republic of the Philippines", new
Font("Tahoma", 20, FontStyle.Bold), Brushes.Pink, new Point(230, 100));
e.Graphics.DrawString("Brgy. San Isidro, Bay Laguna", new
Font("Arial", 15, FontStyle.Bold), Brushes.Blue, new Point(300, 130));

e.Graphics.DrawImage(Properties.Resources._398_3984434_c_language_circle, 800,
100, 200, 100);
int x = 30, y = 170;
// e.Graphics.DrawString("Sample Printing", new Font("Arial", 11,
FontStyle.Bold), Brushes.Black, new Point(30, 50));
//CN.Open();
//Com.CommandText = "select * from sampTbl";
//reader = Com.ExecuteReader();
e.Graphics.DrawString("ID", new Font("Arial", 11, FontStyle.Bold),
Brushes.Black, new Point(30, 150));
e.Graphics.DrawString("STUDENT NAME", new Font("Arial", 11,
FontStyle.Bold), Brushes.Black, new Point(230, 150));
e.Graphics.DrawString("DATE of BIRTH", new Font("Arial", 11,
FontStyle.Bold), Brushes.Black, new Point(430, 150));
e.Graphics.DrawString("AGE", new Font("Arial", 11, FontStyle.Bold),
Brushes.Black, new Point(630, 150));
//while (reader.Read())
//{

for (i = itemsshowed; i < dataGridView1.RowCount; i++)


{
numberofitemsperpage++;

if (numberofitemsperpage <= 3)
{
itemsshowed++;

if (itemsshowed <= dataGridView1.RowCount)


{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].Value.ToString(), new
Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(x, y));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), new
Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(x + 200, y));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), new
Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(x + 400, y));

e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), new
Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(x + 600, y));
y = y + 20;
}
else
{
e.HasMorePages = false;
}
}
else
{
numberofitemsperpage = 0;
e.HasMorePages = true;
return;
}

// reader.Close();
// CN.Close();

e.Graphics.DrawString("TOTAL NO. OF STUDENTS", new


Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(30, 300));
e.Graphics.DrawString("" + dataGridView1.RowCount, new
Font("Arial", 11, FontStyle.Bold), Brushes.Black, new Point(250, 300));

}
}

private void btnPrint_Click(object sender, EventArgs e)


{

PrintPreviewDialog dlg = new PrintPreviewDialog();


dlg.PrintPreviewControl.Zoom = 1.0;
dlg.Document = printDocument1;
this.printDocument1.DefaultPageSettings.Landscape = true;
dlg.ShowDialog();
//printDocument1.Print();
}
}
}
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;
using MySql.Data.MySqlClient;

namespace DBCon4
{
public partial class FormGraph : Form
{
//for connection
MySqlConnection CN = new MySqlConnection("server=localhost;uid=root;
password=''; database=dbCon4");
MySqlCommand Com = new MySqlCommand();
MySqlDataReader reader;

public FormGraph()
{
InitializeComponent();
Com.Connection = CN; ;
}

private void FormGraph_Load(object sender, EventArgs e)


{

ShowGraph();
}

void ShowGraph() {
//chart1.Series["Age"].Points.AddXY("Aaron", 20);
//chart1.Series["Age"].Points.AddXY("Kim", 10);
//chart1.Series["Age"].Points.AddXY("Cora", 50);
//chart1.Series["Age"].Points.AddXY("Benjie", 60);
//chart1.Series["Age"].Points.AddXY("Shane", 30);
CN.Open();
Com.CommandText = "select * from student where dob between '" +
dtStart.Value.ToString("yyyy-MM-dd") + "' AND '" + dtEnd.Value.ToString("yyyy-
MM-dd") + "' order by sname";

reader = Com.ExecuteReader();

while (reader.Read())
{
chart1.Series["Age"].Points.AddXY(reader["sname"],
reader["age"]);
}
reader.Close();
CN.Close();

private void button1_Click(object sender, EventArgs e)


{
ShowGraph();
}
}
}

You might also like