-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListBooks.page.xaml.cs
More file actions
168 lines (143 loc) · 4.8 KB
/
ListBooks.page.xaml.cs
File metadata and controls
168 lines (143 loc) · 4.8 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
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Library_DB
{
/// <summary>
/// Interaction logic for ListBooks.xaml
/// </summary>
public partial class ListBooks : Page
{
MySqlConnection conn;
AddBook ab;
int idB; // book ID- id variable that saves id from row that is being selected
public ListBooks() //Construktor
{
InitializeComponent();
ConnPath();
getTable();
}
public void ConnPath()
{
string filePath = @"C:\Users\tobia\OneDrive\Dokument\GitHub\Library_DB\Resources\lösen.txt";
string passFile = File.ReadAllText(filePath);
string server = "localhost";
string database = "librarydbmodel";
string user = "root";
string pass = passFile;
string connString = $"SERVER={server};DATABASE={database};UID={user};PASSWORD={pass};";
conn = new MySqlConnection(connString);
}
public void getTable(string key ="")
{
//Establera kopplika till Database
string SQLquerry;
if(key == "")
{
SQLquerry = $"CALL SelectBooksWithAuthors();";
}
else
{
SQLquerry = $"CALL Search_Title('{key}');";
}
MySqlCommand cmd = new MySqlCommand(SQLquerry, conn);
try
{
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(reader);
//BooksGrid.ItemSource = dataTable.DefaultView
conn.Close();
BooksGrid.DataContext = dataTable;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
idB= getSelectedRowIDB(sender);
ab = new AddBook(idB);
putRowValues_inTxtbxes(sender);
delButton.IsEnabled = true;
UpdateBtn.IsEnabled = true;
}
private int getSelectedRowIDB(object sender) //gets id of row so that row can be changed
{
DataGrid dg = (DataGrid)sender;
if (dg.SelectedItem is DataRowView rowSelected)
{
int id = Convert.ToInt32(rowSelected.Row.ItemArray[0]);
return id;
}
else
{
return -1;
}
}
private void putRowValues_inTxtbxes(object sender)
{
DataGrid dg = (DataGrid)sender;
if (dg.SelectedItem is DataRowView row_selected)
{
ab.TitleTbx.Text = row_selected["book_title"].ToString();
ab.pageTbx.Text = row_selected["page_amount"].ToString();
ab.authorTbx.Text = row_selected["Author_name"].ToString();
if (ab.favoSlider.Value != 0)
{
int fav = Convert.ToInt32(ab.favoSlider.Value);
fav = Convert.ToInt32(row_selected["favorits_id"].ToString());
ab.favoSlider.IsEnabled = true;
ab.favoCheck.IsChecked = true;
}
}
}
private void DeleteData_click(object sender, RoutedEventArgs e)
{
deleteData(idB);
}
public void deleteData(int id)
{
if (BooksGrid.SelectedItems.Count != 1) return;
try
{
//Öppna kommunimation
conn.Open();
string SQLQuerry = $"CALL DeleteBook({id});";
// MySQL Command
MySqlCommand cmd = new MySqlCommand(SQLQuerry, conn);
//Exekvera command
cmd.ExecuteReader();
MessageBox.Show("Deleted Successfully!");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
getTable();
}
private void updateData_click(object sender, RoutedEventArgs e)
{
Frame.Content = ab;
ab.updateBtn1.IsEnabled =true;
}
}
}