-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
110 lines (97 loc) · 4.73 KB
/
Copy pathForm1.cs
File metadata and controls
110 lines (97 loc) · 4.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
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
namespace Lab06
{
public partial class Form1 : Form
{
// in-memory copy of the Books table
private readonly DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
// ───────────────────────────────────────────────────────────
// 1. Load database when the form starts
// ───────────────────────────────────────────────────────────
private void Form1_Load(object sender, EventArgs e)
{
try
{
string dbPath = Path.Combine(Application.StartupPath, "lib.accdb");
string connStr = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={dbPath};";
string sqlStr = "SELECT * FROM Books";
using (OleDbConnection conn = new OleDbConnection(connStr))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(sqlStr, conn))
{
adapter.Fill(dt);
}
// optional grid for debugging only
dgvBooks.DataSource = dt;
dgvBooks.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show($"Database load error:\n{ex.Message}",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// (still reserved – harmless to keep)
private void dgvBooks_CellContentClick(object sender, DataGridViewCellEventArgs e) { }
private void lstOutput_SelectedIndexChanged(object sender, EventArgs e) { }
// ───────────────────────────────────────────────────────────
// 2. Show Data button – list three columns in lstOutput
// ───────────────────────────────────────────────────────────
private void btnShow_Click(object sender, EventArgs e)
{
string fmt = "{0,-30}{1,-30}{2,-30}";
lstOutput.Items.Clear();
lstOutput.Items.Add(string.Format(fmt, "Title", "Author", "Publisher"));
lstOutput.Items.Add(new string('-', 90));
foreach (DataRow row in dt.Rows)
{
lstOutput.Items.Add(string.Format(fmt,
row["Title"]?.ToString(),
row["Author"]?.ToString(),
row["Publisher"]?.ToString()));
}
}
// ───────────────────────────────────────────────────────────
// 3. Show Status button – pop dialog, display Title/ISBN/Status
// ───────────────────────────────────────────────────────────
private void btnStatus_Click(object sender, EventArgs e)
{
// clear previous display
txtTitle.Clear();
txtISBN.Clear();
txtStatus.Clear();
using (frmSearch frm = new frmSearch())
{
if (frm.ShowDialog() == DialogResult.OK)
{
string keyword = (frm.Result ?? string.Empty).Trim().ToUpper();
bool found = false;
foreach (DataRow row in dt.Rows)
{
// null-safe read of Title
string title = (row["Title"]?.ToString() ?? string.Empty).ToUpper();
if (title.Equals(keyword))
{
txtTitle.Text = row["Title"]?.ToString();
txtISBN.Text = row["ISBN"]?.ToString();
txtStatus.Text = row["Status"]?.ToString();
found = true;
break;
}
}
if (!found)
MessageBox.Show("No such book can be found.",
"Not found", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
}