-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
157 lines (141 loc) · 5.73 KB
/
Form1.cs
File metadata and controls
157 lines (141 loc) · 5.73 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Npgsql;
using System.Data;
namespace Modul_9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private NpgsqlConnection conn;
string connstring = "Host=localhost;Port=5434;Username=postgres;Password=postgres;Database=ListOfView";
// public static NpgsqlConnection conn = new NpgsqlConnection(ConnectionString: connstring);
public DataTable dt;
public static NpgsqlCommand cmd;
private string sql = null;
private DataGridViewRow r;
private void btnQR_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new NpgsqlConnection(connstring);
}
private void btnLoad_Click(object sender, EventArgs e)
{
try
{
conn.Open();
dgv.DataSource = null;
sql = "select * from st_select()";
cmd = new NpgsqlCommand(sql, conn);
dt = new DataTable();
NpgsqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
dgv.DataSource = dt;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "FAIL!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnInsert_Click(object sender, EventArgs e)
{
try
{
conn.Open();
sql = @"select * from st_insert(:_name, :_alamat,:_no_hp)";
cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("_name", tbNama.Text);
cmd.Parameters.AddWithValue("_alamat", tbAlamat.Text);
cmd.Parameters.AddWithValue("_no_hp", tbHp.Text);
if ((int)cmd.ExecuteScalar() == 1)
{
MessageBox.Show("Data Users Berhasil diinputkan", "Well Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
btnLoad.PerformClick();
tbNama.Text = tbHp.Text = tbAlamat.Text = null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Insert FAIL!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
r = dgv.Rows[e.RowIndex];
tbNama.Text = r.Cells["_name"].Value.ToString();
tbAlamat.Text = r.Cells["_alamat"].Value.ToString();
tbHp.Text = r.Cells["_no_hp"].Value.ToString();
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (r == null)
{
MessageBox.Show("Mohon pilih baris data yang akan diupdate", "Good!", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
conn.Open();
sql = @"select * from st_update(:_id, :_name, :_alamat, :_no_hp)";
cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("_id", r.Cells["_id"].Value.ToString());
cmd.Parameters.AddWithValue("_name", tbNama.Text);
cmd.Parameters.AddWithValue("_alamat", tbAlamat.Text);
cmd.Parameters.AddWithValue("_no_hp", tbHp.Text);
if ((int)cmd.ExecuteScalar() == 1)
{
MessageBox.Show("Data Users Berhasil diupdate", "Well Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
btnLoad.PerformClick();
tbNama.Text = tbHp.Text = tbAlamat.Text = null;
r = null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Update FAIL!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (r == null)
{
MessageBox.Show("Mohon pilih baris data yang akan diupdate", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (MessageBox.Show("Apakah benar anda ingin menghapus data" + r.Cells["_name"].Value.ToString() + " ?", "Hapus data terkonfirmasi",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
try
{
conn.Open();
sql = @"select * from st_delete(:_id)";
cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("_id", r.Cells["_id"].Value.ToString());
if ((int)cmd.ExecuteScalar() == 1)
{
MessageBox.Show("Data Users Berhasil dihapus", "Well Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
btnLoad.PerformClick();
tbNama.Text = tbHp.Text = tbAlamat.Text = null;
r = null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Delete FAIL!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}