Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Code

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

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 System.Data.SqlClient;

namespace Activity_Simple_System
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\
v11.0;AttachDbFilename=E:\Alarms\Jomar Basic System\Activity Simple System\
Activity Simple System\Database2.mdf;Integrated Security=True");
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText="insert into[Table]
(Name,Age,Address,Contact)values('" + textBox1.Text + "','" + textBox2.Text +
"','" + textBox3.Text + "','" + textBox4.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
MessageBox.Show("inserted successfull");
}
public void ClearData()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}

private void button2_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from [Table] where Name LIKE '" +
textBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
cmd.Parameters.AddWithValue("Name", textBox1.Text);
cmd.Parameters.AddWithValue("Age", textBox2.Text);
cmd.Parameters.AddWithValue("Address", textBox3.Text);
cmd.Parameters.AddWithValue("Contact", textBox4.Text);
MessageBox.Show("data deleted successfull");
ClearData();
}

private void button4_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update [Table] set Name ='" + textBox1.Text +
"' where Age= '" + textBox2.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
cmd.Parameters.AddWithValue("Name", textBox1.Text);
cmd.Parameters.AddWithValue("Age", textBox2.Text);
cmd.Parameters.AddWithValue("Address", textBox3.Text);
cmd.Parameters.AddWithValue("Contact", textBox4.Text);
MessageBox.Show("Update Succes");
ClearData();

}
public void display_data()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [Table]";
cmd.ExecuteNonQuery();
DataTable dta = new DataTable();
SqlDataAdapter dataadp = new SqlDataAdapter(cmd);
dataadp.Fill(dta);
dataGridView1.DataSource = dta;
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
display_data();
}

private void button5_Click(object sender, EventArgs e)


{
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter("select * from [Table]
where Name like '" + textBox5.Text + "%'", con);
DataTable dt = new DataTable();
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
textBox1.Text =
dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text =
dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text =
dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text =
dataGridView1.SelectedRows[0].Cells[3].Value.ToString();

private void printDocument1_PrintPage(object sender,


System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap print = new Bitmap(this.dataGridView1.Width,
dataGridView1.Height);
dataGridView1.DrawToBitmap(print, new Rectangle(0, 0,
this.dataGridView1.Width, dataGridView1.Height));

e.Graphics.DrawImage(print, 100, 120);


e.Graphics.DrawString(label1.Text, new Font("Verdana", 22,
FontStyle.Bold), Brushes.Pink, new Point(100, 60));
}

private void button6_Click(object sender, EventArgs e)


{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();

private void printPreviewDialog1_Load(object sender, EventArgs e)


{

}
}
}

You might also like