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

Code

The document details code for a C# Windows Forms application that performs CRUD operations on a SQL Server database table using ADO.NET. The application allows inserting, deleting, updating and searching database records, and displaying them in a datagrid. It also implements printing of the datagrid contents.

Uploaded by

Joms Pascual
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)
31 views

Code

The document details code for a C# Windows Forms application that performs CRUD operations on a SQL Server database table using ADO.NET. The application allows inserting, deleting, updating and searching database records, and displaying them in a datagrid. It also implements printing of the datagrid contents.

Uploaded by

Joms Pascual
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/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy