-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
135 lines (118 loc) · 4.68 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentCRUDApplication
{
public partial class Form1 : Form
{
SqlConnection connection;
private int tempID;
public Form1()
{
InitializeComponent();
connection = new SqlConnection(@"Data Source = VWVITHLPUB1701\SQLEXPRESS; Initial Catalog = Student; Integrated Security = True;");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Label2_Click(object sender, EventArgs e)
{
}
private void TextBox4_TextChanged(object sender, EventArgs e)
{
}
private void Button2_Click(object sender, EventArgs e)
{
display();
}
public void display()
{
SqlDataAdapter sqlAdapter = new SqlDataAdapter("Select * from tbl_student",connection);
DataTable dt = new DataTable();
sqlAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
public void clearFields()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
comboBox1.Text = "";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd;
connection.Open();
string sqlQuery = "Insert INTO tbl_student (Name,Class,Section,Percentage,Gender) VALUES (@p1,@p2,@p3,@p4,@p5)";
cmd = new SqlCommand(sqlQuery, connection);
cmd.Parameters.AddWithValue("@p1", textBox1.Text);
cmd.Parameters.AddWithValue("@p2", int.Parse(textBox2.Text));
cmd.Parameters.AddWithValue("@p3", textBox3.Text);
cmd.Parameters.AddWithValue("@p4", int.Parse(textBox4.Text));
cmd.Parameters.AddWithValue("@p5", comboBox1.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
display();
MessageBox.Show("Data Inserted Successfully");
}
private void Button3_Click(object sender, EventArgs e)
{
connection.Open();
for(int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow delRow = dataGridView1.Rows[i];
if(delRow.Selected == true)
{
string sqlQuery = "DELETE FROM tbl_student Where id='" + dataGridView1.Rows[i].Cells[0].Value + "'";
SqlCommand sqlCommand = new SqlCommand(sqlQuery, connection);
sqlCommand.ExecuteNonQuery();
dataGridView1.Rows.RemoveAt(i);
}
}
connection.Close();
MessageBox.Show("Data Deleted Successfully");
}
private void DataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(dataGridView1.Rows.Count > 0)
{
tempID = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
}
}
private void Button4_Click(object sender, EventArgs e)
{
SqlCommand cmd;
connection.Open();
string sqlQuery = "UPDATE tbl_student set Name=@p1,Class=@p2,Section=@p3,Percentage=@p4,Gender=@p5 where id='"+tempID+"'";
cmd = new SqlCommand(sqlQuery, connection);
cmd.Parameters.AddWithValue("@p1", textBox1.Text);
cmd.Parameters.AddWithValue("@p2", int.Parse(textBox2.Text));
cmd.Parameters.AddWithValue("@p3", textBox3.Text);
cmd.Parameters.AddWithValue("@p4", int.Parse(textBox4.Text));
cmd.Parameters.AddWithValue("@p5", comboBox1.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
display();
clearFields();
MessageBox.Show("Data Updated Successfully");
}
}
}