-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
342 lines (267 loc) · 12.1 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Windows.Forms;
namespace phoneBook
{
public partial class PhoneBook : Form
{
public void loadData()
{
SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True");
con.Open();
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM TablePhoneBook", con);
DataTable dt = new DataTable();
ad.Fill(dt);
dgvPhoneBook.DataSource = dt;
//SqlCommand cmd = new SqlCommand("SELECT * FROM TablePhoneBook", con);
//SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
//sqlDataAdapter.SelectCommand = cmd;
//DataTable dataTable = new DataTable();
//sqlDataAdapter.Fill(dataTable);
//BindingSource bindingSource = new BindingSource();
//bindingSource.DataSource = dataTable;
//dgvPhoneBook.DataSource = bindingSource;
//sqlDataAdapter.Update(dataTable);
//con.Close();
//SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True");
//con.Open();
//String query = "select * from TablePhoneBook";
//SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query,con);
//var ds=new DataSet();
//dgvPhoneBook.DataSource = ds.Tables[0];
//con.Close();
}
public PhoneBook()
{
InitializeComponent();
//loadData();
}
private void PhoneBook_Load(object sender, EventArgs e)
{
loadData();
}
private void Apppanel1_Paint(object sender, PaintEventArgs e)
{
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True"))
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO TablePhoneBook ([sID], [firstName], [lastName], [email], [mobile]) VALUES (@sID, @firstName, @lastName, @email, @mobile)", con);
cmd.Parameters.AddWithValue("@sID", textID.Text);
cmd.Parameters.AddWithValue("@firstName", textFirstName.Text);
cmd.Parameters.AddWithValue("@lastName", textLastName.Text);
cmd.Parameters.AddWithValue("@email", textEmail.Text);
cmd.Parameters.AddWithValue("@mobile", textMobile.Text);
cmd.ExecuteNonQuery(); // Execute the insert command
MessageBox.Show("Add success", "Message Title", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData(); // Refresh data or load updated data, if applicable
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void dgvPhoneBook_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Check if a row is actually selected
if (e.RowIndex >= 0)
{
DataGridViewRow row = dgvPhoneBook.Rows[e.RowIndex];
// Populate the text boxes with data from the selected row's cells
textID.Text = row.Cells["sID"].Value.ToString();
textFirstName.Text = row.Cells["firstName"].Value.ToString();
textLastName.Text = row.Cells["lastName"].Value.ToString();
textEmail.Text = row.Cells["email"].Value.ToString();
textMobile.Text = row.Cells["mobile"].Value.ToString();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textID.Text))
{
MessageBox.Show("Please enter a valid ID.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
using (SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM TablePhoneBook WHERE sID = @sID", con);
cmd.Parameters.AddWithValue("@sID", textID.Text);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Delete success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData(); // Refresh data or load updated data
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
textID.Text = string.Empty;
textFirstName.Text = string.Empty;
textLastName.Text = string.Empty;
textEmail.Text = string.Empty;
textMobile.Text = string.Empty;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True"))
{
con.Open();
// Update query with parameters for multiple fields
SqlCommand cmd = new SqlCommand(
"UPDATE TablePhoneBook SET firstName = @firstName, lastName = @lastName, email = @email,mobile = @mobile WHERE sID = @sID", con);
// Add parameters for each field
cmd.Parameters.AddWithValue("@sID", textID.Text);
cmd.Parameters.AddWithValue("@firstName", textFirstName.Text);
cmd.Parameters.AddWithValue("@lastName", textLastName.Text);
cmd.Parameters.AddWithValue("@email", textEmail.Text);
cmd.Parameters.AddWithValue("@mobile", textMobile.Text);
// Execute the query and check if any rows were affected
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Update successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData(); // Refresh data or load updated data
}
else
{
MessageBox.Show("No record found to update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//private void btnSearch_Click(object sender, EventArgs e)
//{
// string searchID = txtSearch.Text.Trim(); // Get the search term
// SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True");
// // Open the connection
// con.Open();
// // If no search term, load all data
// if (string.IsNullOrWhiteSpace(searchID))
// {
// SqlCommand cmd = new SqlCommand("SELECT * FROM TablePhoneBook", con);
// SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
// DataTable dt = new DataTable();
// sqlDataAdapter.Fill(dt);
// dgvPhoneBook.DataSource = dt; // Bind all data to DataGridView
// }
// else
// {
// // If search term exists, search by sID
// SqlCommand cmd = new SqlCommand("SELECT * FROM TablePhoneBook WHERE sID = @sID", con);
// cmd.Parameters.AddWithValue("@sID", searchID);
// SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
// DataTable dt = new DataTable();
// sqlDataAdapter.Fill(dt);
// dgvPhoneBook.DataSource = dt; // Bind filtered data to DataGridView
// }
// con.Close(); // Close the connection
//}
private void btnSearch_Click(object sender, EventArgs e)
{
SearchData(txtSearch.Text.Trim());
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
SearchData(txtSearch.Text.Trim()); // Real-time search as user types
}
private void SearchData(string searchTerm)
{
using (SqlConnection con = new SqlConnection("Data Source=LAPTOPJK\\SQLEXPRESS05;Database=PhoneBookDB;Integrated Security=True"))
{
try
{
con.Open();
SqlCommand cmd;
if (string.IsNullOrWhiteSpace(searchTerm))
{
// Load all data if search box is empty
cmd = new SqlCommand("SELECT * FROM TablePhoneBook", con);
}
else
{
// Search across multiple fields with partial matching
cmd = new SqlCommand(@"
SELECT * FROM TablePhoneBook
WHERE sID LIKE @search
OR firstName LIKE @search
OR lastName LIKE @search
OR email LIKE @search
OR mobile LIKE @search", con);
cmd.Parameters.AddWithValue("@search", "%" + searchTerm + "%");
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sqlDataAdapter.Fill(dt);
// Set the DataSource
dgvPhoneBook.DataSource = dt;
// Show a message if no results found
if (dt.Rows.Count == 0 && !string.IsNullOrWhiteSpace(searchTerm))
{
MessageBox.Show("No matching records found.", "Search Results",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("Error during search: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
int currentIndex = 0; // Variable to store the current row index
private void ShowData(int index)
{
// Check if the index is within bounds
if (index >= 0 && index < dgvPhoneBook.Rows.Count)
{
dgvPhoneBook.ClearSelection();
dgvPhoneBook.Rows[index].Selected = true;
dgvPhoneBook.CurrentCell = dgvPhoneBook.Rows[index].Cells[0];
currentIndex = index;
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
ShowData(0); // Show the first row
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (currentIndex > 0)
{
ShowData(currentIndex - 1); // Show the previous row
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (currentIndex < dgvPhoneBook.Rows.Count - 1)
{
ShowData(currentIndex + 1); // Show the next row
}
}
private void btnLast_Click(object sender, EventArgs e)
{
ShowData(dgvPhoneBook.Rows.Count - 2); // Show the last row
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}