-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormCarti.cs
More file actions
331 lines (296 loc) · 13.3 KB
/
FormCarti.cs
File metadata and controls
331 lines (296 loc) · 13.3 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
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
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;
using System.IO;
namespace Proiect_ImprumuturiBiblioteca
{
public partial class FormCarti : Form
{
Carte c;
SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSqlLocalDB;Initial Catalog=DB-ProiectPAW;Integrated Security=True");
SqlCommand command;
DataTable dataTable;
SqlDataAdapter adapter;
DataSet dataSet;
public FormCarti()
{
InitializeComponent();
}
/////////////////////////INSERT/UPDATE/////////////////////////
private void buttonAdauga_Click(object sender, EventArgs e)
{
try
{
connection.Open();
SqlCommand command;
//inserare in baza de date (INSERT)
if (buttonAdauga.Text == "&Adauga")
{
command = new SqlCommand("insert into dbo.carti (Titlu, Autor, Editura, Categorie) values (@Titlu, @Autor, @Editura, @Categorie)", connection);
command.Parameters.AddWithValue("@Titlu", textBoxTitlu.Text);
command.Parameters.AddWithValue("@Autor", textBoxAutor.Text);
command.Parameters.AddWithValue("@Editura", comboBoxEditura.SelectedItem.ToString());
command.Parameters.AddWithValue("@Categorie", comboBoxCategoria.SelectedItem.ToString());
command.ExecuteNonQuery();
MessageBox.Show("Cartea s-a adaugat cu succes!", "Confirmare", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//actualizarea datelor in baza de date (UPDATE)
else
{
command = new SqlCommand("update dbo.carti set Titlu=@Titlu, Autor=@Autor, Editura=@Editura, Categorie=@Categorie where Cod_carte=@Cod_carte", connection);
command.Parameters.AddWithValue("@Titlu", textBoxTitlu.Text);
command.Parameters.AddWithValue("@Autor", textBoxAutor.Text);
command.Parameters.AddWithValue("@Editura", comboBoxEditura.SelectedItem.ToString());
command.Parameters.AddWithValue("@Categorie", comboBoxCategoria.SelectedItem.ToString());
c = (Carte)listViewCarti.SelectedItems[0].Tag;
int Cod_carte = c.Cod;
command.Parameters.AddWithValue("@Cod_carte", Cod_carte);
command.ExecuteNonQuery();
MessageBox.Show("Detaliile cartii s-au actualizat cu succes!", "Confirmare", MessageBoxButtons.OK, MessageBoxIcon.Information);
buttonAdauga.Text = "&Adauga";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connection.Close();
textBoxTitlu.Text = "";
textBoxAutor.Text = "";
comboBoxEditura.Text = "";
comboBoxCategoria.Text = "";
}
buttonAfisCarti_Click_1(sender, e);
}
/////////////////////////afisarea cartilor din DB in listview (SELECT)/////////////////////////
private void buttonAfisCarti_Click_1(object sender, EventArgs e)
{
listViewCarti.Items.Clear();
try
{
connection.Open();
command = new SqlCommand("select * from dbo.carti", connection);
adapter = new SqlDataAdapter(command);
dataSet = new DataSet();
adapter.Fill(dataSet, "Carti");
dataSet.Tables["Carti"].PrimaryKey = new DataColumn[1] { dataSet.Tables["Carti"].Columns["Cod_carte"] };
dataTable = dataSet.Tables["Carti"];
List<Carte> carti = new List<Carte>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
carti.Add(new Carte(Convert.ToInt32(dataTable.Rows[i].ItemArray[0]), dataTable.Rows[i].ItemArray[1].ToString(), dataTable.Rows[i].ItemArray[2].ToString(), dataTable.Rows[i].ItemArray[3].ToString(), dataTable.Rows[i].ItemArray[4].ToString()));
}
foreach (Carte c in carti)
{
ListViewItem lvi = new ListViewItem(c.Cod.ToString());
lvi.SubItems.Add(c.Titlu);
lvi.SubItems.Add(c.Autor);
lvi.SubItems.Add(c.Editura);
lvi.SubItems.Add(c.Categorie);
lvi.Tag = c;
listViewCarti.Items.Add(lvi);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connection.Close();
}
}
/////////////////////////editare date/////////////////////////
private void editeazaToolStripMenuItem_Click(object sender, EventArgs e)
{
preluareDate();
buttonAdauga.Text = "&Salveaza";
}
/////////////////////////stergerea din BD (DELETE)/////////////////////////
private void stergeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listViewCarti.CheckedItems.Count == 1)
{
c = (Carte)listViewCarti.CheckedItems[0].Tag;
DialogResult confirmare = MessageBox.Show("Sigur doriti sa stergeti cartea '" + c.Titlu + "'?", "Confirmare stergere", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmare == DialogResult.Yes)
{
connection.Open();
command = new SqlCommand("delete from dbo.carti where Cod_carte=" + c.Cod, connection);
command.ExecuteNonQuery();
connection.Close();
buttonAfisCarti_Click_1(sender, e);
}
}
else
if (listViewCarti.CheckedItems.Count > 1)
{
DialogResult confirmare = MessageBox.Show("Sigur doriti sa stergeti cele " + listViewCarti.CheckedItems.Count + " carti?", "Confirmare stergere", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmare == DialogResult.Yes)
{
connection.Open();
for (int i = 0; i < listViewCarti.Items.Count; i++)
{
if (listViewCarti.Items[i].Checked)
{
command = new SqlCommand("delete from dbo.carti where Cod_carte=@Cod_carte", connection);
int Cod_carte = Convert.ToInt32(listViewCarti.Items[i].SubItems[0].Text);
command.Parameters.AddWithValue("@Cod_carte", Cod_carte);
command.ExecuteNonQuery();
}
}
connection.Close();
buttonAfisCarti_Click_1(sender, e);
}
}
}
/////////////////////////preluarea datelor din Form1/////////////////////////
public void preluareDate()
{
c = (Carte)listViewCarti.SelectedItems[0].Tag;
textBoxTitlu.Text = c.Titlu;
textBoxAutor.Text = c.Autor;
comboBoxEditura.Text = c.Editura;
comboBoxCategoria.Text = c.Categorie;
}
/////////////////////////validarea datelor intorduse/////////////////////////
//pot inchide chiar daca nu am completat nimic --> (CausesValidation pe False)
private void textBoxTitlu_Validating(object sender, CancelEventArgs e)
{
if (textBoxTitlu.Text == "")
{
errorProvider1.SetError(textBoxTitlu, "Introduceti un titlu!");
e.Cancel = true;
}
else
errorProvider1.SetError(textBoxTitlu, "");
}
private void textBoxAutor_Validating(object sender, CancelEventArgs e)
{
if (textBoxAutor.Text == "")
{
errorProvider1.SetError(textBoxAutor, "Introduceti un autor!");
e.Cancel = true;
}
else
errorProvider1.SetError(textBoxAutor, "");
}
//numele autorului sa contina doar spatii, litere, . sau -
private void textBoxAutor_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLetter(e.KeyChar) == true || char.IsWhiteSpace(e.KeyChar) == true || char.IsControl(e.KeyChar) == true || e.KeyChar == '.' || e.KeyChar == '-')
e.Handled = false;
else
e.Handled = true;
}
private void comboBoxEditura_Validating(object sender, CancelEventArgs e)
{
if (comboBoxEditura.Text == "")
{
errorProvider1.SetError(comboBoxEditura, "Alegeti o editura!");
e.Cancel = true;
}
else
errorProvider1.SetError(comboBoxEditura, "");
}
private void comboBoxCategoria_Validating(object sender, CancelEventArgs e)
{
if (comboBoxCategoria.Text == "")
{
errorProvider1.SetError(comboBoxCategoria, "Alegeti o categorie!");
e.Cancel = true;
}
else
errorProvider1.SetError(comboBoxCategoria, "");
}
/////////////////////////dezactivarea/activarea controalelor/////////////////////////
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (listViewCarti.SelectedItems.Count == 1)
{
editeazaToolStripMenuItem.Enabled = true;
}
else
{
editeazaToolStripMenuItem.Enabled = false;
}
if (listViewCarti.CheckedItems.Count > 0)
{
stergeToolStripMenuItem.Enabled = true;
}
else
{
stergeToolStripMenuItem.Enabled = false;
}
}
/////////////////////////salvare in fisier text/////////////////////////
private void salveazaToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.CheckPathExists = true;
saveFile.Filter = "(*.txt)|*.txt";
if (saveFile.ShowDialog() == DialogResult.OK)
{
FileStream file = new FileStream(saveFile.FileName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(file);
foreach (ListViewItem item in listViewCarti.Items)
{
writer.WriteLine(item.SubItems[0].Text + ";" + item.SubItems[1].Text + ";" + item.SubItems[2].Text + ";" + item.SubItems[3].Text + ";" + item.SubItems[4].Text);
}
MessageBox.Show("Fisier text generat!", "Confirmare", MessageBoxButtons.OK, MessageBoxIcon.Information);
writer.Close();
file.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/////////////////////////citire din fisier text/////////////////////////
private void deschideToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.CheckPathExists = true;
openFile.CheckFileExists = true;
openFile.Filter = "(*.txt)|*.txt";
if (openFile.ShowDialog() == DialogResult.OK)
{
FileStream file = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
listViewCarti.Items.Clear();
string line;
while ((line = reader.ReadLine()) != null)
{
string[] vectorLine = line.Split(new char[1] { ';' });
ListViewItem item = new ListViewItem(vectorLine[0]);
item.SubItems.Add(vectorLine[1]);
item.SubItems.Add(vectorLine[2]);
item.SubItems.Add(vectorLine[3]);
item.SubItems.Add(vectorLine[4]);
listViewCarti.Items.Add(item);
}
reader.Close();
file.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}