-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserPanel.cs
154 lines (124 loc) · 4.59 KB
/
UserPanel.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
using MySql.Data.MySqlClient;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace CS322___Projektni_zadatak___Bojana___Stajić___4596
{
public partial class UserPanel : Form
{
MySqlConnection mycon;
string con = "datasource=localhost;port=3306;username=root;password='';database=cs322_pz";
DataTable dt = new DataTable();
public UserPanel()
{
InitializeComponent();
}
private void UserPanel_Load(object sender, EventArgs e)
{
try
{
mycon = new MySqlConnection(con);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM books;", mycon);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bind = new BindingSource();
bind.DataSource = dt;
dgvBooks.DataSource = bind;
adapter.Update(dt);
dgvBooks.Columns[0].Visible = false;
dgvBooks.AutoResizeColumns();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
public void searchBook(string bookToFind)
{
string searchQuery = "SELECT * FROM books WHERE title LIKE '%" + bookToFind + "%'";
MySqlDataAdapter adapter = new MySqlDataAdapter(searchQuery, con);
DataTable table = new DataTable();
adapter.Fill(table);
dgvBooks.DataSource = table;
}
private void tbSearch_TextChanged(object sender, EventArgs e)
{
searchBook(tbSearch.Text);
}
private void UserPanel_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void tbBookToAdd_TextChanged(object sender, EventArgs e)
{
}
private void dgvBooks_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvBooks.Rows[e.RowIndex];
book_id = int.Parse(row.Cells["id"].Value.ToString());
tbBookToAdd.Text = row.Cells["title"].Value.ToString();
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Hide();
Cart cartForm = new Cart();
cartForm.Show();
cartForm.FormClosed += (s, args) => this.Close();
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
if (book_id == 0)
{
MessageBox.Show("Please select a book in order to buy it.");
}
else
{
mycon = new MySqlConnection(con);
mycon.Open();
string query = "INSERT INTO cart_items(book_id, user_id) VALUES(@book_id,@user_id)";
MySqlCommand cmd = new MySqlCommand(query, mycon);
cmd.Parameters.AddWithValue("@book_id", book_id);
cmd.Parameters.AddWithValue("@user_id", Shared.user_id);
cmd.ExecuteScalar();
MessageBox.Show("User! You have successfully added a book to your cart!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//
}
private void pictureBox3_Click(object sender, EventArgs e)
{
this.Hide();
UserBooks userBooksForm = new UserBooks();
userBooksForm.Show();
userBooksForm.FormClosed += (s, args) => this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Hide();
Login homePanel = new Login();
homePanel.Show();
homePanel.FormClosed += (s, args) => this.Close();
}
}
}