Advertisement
Guest User

asd

a guest
Mar 9th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 55.70 KB | None | 0 0
  1. Connection.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using MySql.Data.MySqlClient;
  7.  
  8. namespace LibrarySystem
  9. {
  10. class Connection
  11. {
  12. private static string server = "";
  13. private static string database = "";
  14. private static string uid = "";
  15. private static string password = "";
  16. private static string connstr = "";
  17.  
  18. public static string getConnectionStr(){
  19. return connstr = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  20. }
  21.  
  22.  
  23. public static void setServer(string server)
  24. {
  25. Connection.server = server;
  26. }
  27. public static void setDatabase(string database)
  28. {
  29. Connection.database = database;
  30. }
  31.  
  32. public static void setUid(string uid)
  33. {
  34. Connection.uid = uid;
  35. }
  36. public static void setPassword(string pass)
  37. {
  38. Connection.password = pass;
  39. }
  40. }
  41. }
  42.  
  43. DatabaseHelper.cs
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Linq;
  47. using System.Text;
  48. using MySql.Data.MySqlClient;
  49. using System.Windows.Forms;
  50. namespace LibrarySystem
  51. {
  52. class DatabaseHelper
  53. {
  54. MySqlConnection con;
  55.  
  56. public DatabaseHelper(string connstr)
  57. {
  58. con = new MySqlConnection(connstr);
  59.  
  60. }
  61.  
  62. public void Instantiate()
  63. {
  64.  
  65.  
  66.  
  67. }
  68.  
  69. public MySqlConnection getConnection()
  70. {
  71. return this.con;
  72. }
  73.  
  74. public void executecmd(string query)
  75. {
  76. MySqlCommand cmd = new MySqlCommand(query, getConnection());
  77. cmd.ExecuteNonQuery();
  78.  
  79.  
  80. }
  81. public bool openConnection()
  82. {
  83. try
  84. {
  85. con.Open();
  86. return true;
  87. }
  88. catch (MySqlException ex)
  89. {
  90. MessageBox.Show(ex.Message.ToString());
  91. return false;
  92. }
  93. }
  94.  
  95.  
  96. public bool CloseConnection()
  97. {
  98. try
  99. {
  100. con.Close();
  101. return true;
  102. }
  103. catch (MySqlException ex)
  104. {
  105. MessageBox.Show(ex.Message);
  106. return false;
  107. }
  108. }
  109. }
  110.  
  111. }
  112.  
  113. frmForgot.cs
  114. using System;
  115. using System.Collections.Generic;
  116. using System.ComponentModel;
  117. using System.Data;
  118. using System.Drawing;
  119. using System.Linq;
  120. using System.Text;
  121. using System.Windows.Forms;
  122. using MySql.Data.MySqlClient;
  123. namespace LibrarySystem
  124. {
  125. public partial class frmForgot : Form
  126. {
  127. DatabaseHelper dbhelper;
  128. public frmForgot()
  129. {
  130. InitializeComponent();
  131. dbhelper = new DatabaseHelper(Connection.getConnectionStr());
  132. }
  133. private string answer = "";
  134.  
  135. private void txtanswer_TextChanged(object sender, EventArgs e)
  136. {
  137.  
  138. }
  139.  
  140.  
  141. private void txtusername_TextChanged(object sender, EventArgs e)
  142. {
  143. dbhelper.openConnection();
  144. string query = "SELECT Question, Answer from answersView WHERE user_name=@user";
  145. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  146. cmd.Parameters.AddWithValue("user", txtusername.Text);
  147. MySqlDataReader reader = cmd.ExecuteReader();
  148. int count = 0;
  149. while (reader.Read())
  150. {
  151. count++;
  152. if (count == 1)
  153. {
  154. txtquestion.Text = "Question: " + reader["Question"].ToString();
  155. answer = reader["Answer"].ToString();
  156. }
  157. }
  158. if (count == 0)
  159. {
  160. txtquestion.Text = "Username: " + txtusername.Text + " not found in database!";
  161. answer = "";
  162. }
  163. dbhelper.CloseConnection();
  164. }
  165.  
  166. private void btnRetrieve_Click(object sender, EventArgs e)
  167. {
  168. if (txtanswer.Text == answer)
  169. {
  170. if(txtpassword.Text == txtnewpassword.Text)
  171. {
  172. if(txtpassword.Text != "")
  173. {
  174. if (dbhelper.openConnection())
  175. {
  176. try
  177. {
  178.  
  179. string query = "UPDATE tblaccounts SET pass_word = @pass WHERE user_name = @user";
  180. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  181. cmd.Parameters.AddWithValue("user", txtusername.Text);
  182. cmd.Parameters.AddWithValue("pass", MD5Hasher.GetMd5Hash(txtnewpassword.Text));
  183. cmd.ExecuteNonQuery();
  184. MessageBox.Show("Your password is changed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  185. }
  186. catch (Exception ex)
  187. {
  188. MessageBox.Show(ex.Message);
  189. }
  190. }
  191.  
  192. }
  193. else
  194. {
  195. MessageBox.Show("New password cant\'t be blank!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  196.  
  197. }
  198.  
  199.  
  200. }
  201. else
  202. {
  203. MessageBox.Show("New password mismatched!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  204. }
  205. dbhelper.CloseConnection();
  206. }
  207. else
  208. {
  209. MessageBox.Show("Answer mismatched!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  210. }
  211. }
  212. }
  213. }
  214.  
  215. frmLibrarian.cs
  216. using System;
  217. using System.Collections.Generic;
  218. using System.ComponentModel;
  219. using System.Data;
  220. using System.Drawing;
  221. using System.Linq;
  222. using System.Text;
  223. using System.Windows.Forms;
  224. using MySql.Data.MySqlClient;
  225. namespace LibrarySystem
  226. {
  227. public partial class frmLibrarian : Form
  228. {
  229. DatabaseHelper dbhelper;
  230. DataTable books;
  231. DataTable borrowedBooks;
  232. string accid = "";
  233. bool editing = false;
  234. string tempsection = "";
  235. string tempgrade= "";
  236. public frmLibrarian()
  237. {
  238. InitializeComponent();
  239. Connection.setServer("127.0.0.1");
  240. Connection.setDatabase("dblibrary");
  241. Connection.setUid("root");
  242. Connection.setPassword("");
  243.  
  244. dbhelper = new DatabaseHelper(Connection.getConnectionStr());
  245. loadBooks();
  246. addEditButton();
  247. getName();
  248. loadAccountInfo();
  249. loadBorrowedBooks();
  250. loadsecurity();
  251.  
  252. cboagrade.DropDownStyle = ComboBoxStyle.DropDownList;
  253. cboasecurity.DropDownStyle = ComboBoxStyle.DropDownList;
  254. cbotype.DropDownStyle = ComboBoxStyle.DropDownList;
  255.  
  256. cbotype.SelectedIndex = 0;
  257. cboasecurity.SelectedIndex = 0;
  258. cboagrade.SelectedIndex = 0;
  259.  
  260. loadaedAccounts("student", "");
  261. }
  262.  
  263. private void loadBooks()
  264. {
  265.  
  266. if (dbhelper.openConnection())
  267. {
  268. string query = "SELECT * FROM tblbooks";
  269. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  270. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  271. books = new DataTable();
  272. da.Fill(books);
  273. dgvBooks.DataSource = books;
  274. dgvAddBook.DataSource = books;
  275. dbhelper.CloseConnection();
  276. }
  277. if (dbhelper.openConnection())
  278. {
  279. int noOfDistinctBook = 0;
  280. string query = "Select COUNT(*) as NoOfBooks from tblbooks";
  281. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  282. MySqlDataReader reader = cmd.ExecuteReader();
  283. while (reader.Read())
  284. {
  285. noOfDistinctBook = int.Parse(reader["NoOfBooks"].ToString());
  286. }
  287. dbhelper.CloseConnection();
  288. lblbookcount.Text = "Book Count: " + noOfDistinctBook.ToString();
  289. }
  290. }
  291.  
  292. private void getName()
  293. {
  294. if (dbhelper.openConnection())
  295. {
  296. string fullname = "";
  297. string query = "Select CONCAT(last_name, ',' , first_name, ' ', middle_name) as FullName from tbllibrarian WHERE AccountID=@id";
  298. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  299. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  300. MySqlDataReader reader = cmd.ExecuteReader();
  301. while (reader.Read())
  302. {
  303. fullname = reader["Fullname"].ToString();
  304. }
  305. dbhelper.CloseConnection();
  306. lblWelcome.Text = "Welcome: " + fullname;
  307. }
  308. }
  309.  
  310. private void frmLibrarian_FormClosing(object sender, FormClosingEventArgs e)
  311. {
  312. DialogResult dr = MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  313. if (dr == DialogResult.No)
  314. {
  315. e.Cancel = true;
  316.  
  317. }
  318. }
  319.  
  320. private void btnLogout_Click(object sender, EventArgs e)
  321. {
  322. this.Close();
  323. }
  324.  
  325. private void loadAccountInfo()
  326. {
  327. try
  328. {
  329. if (dbhelper.openConnection())
  330. {
  331. string query = "SELECT * FROM tbllibrarian WHERE AccountID= @id";
  332. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  333. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  334. MySqlDataReader reader = cmd.ExecuteReader();
  335.  
  336. while (reader.Read())
  337. {
  338. fname.Text = reader["first_name"].ToString();
  339. mname.Text = reader["middle_name"].ToString();
  340. lname.Text = reader["last_name"].ToString();
  341. }
  342. dbhelper.CloseConnection();
  343. }
  344.  
  345. }
  346. catch (Exception ex)
  347. {
  348. MessageBox.Show(ex.ToString());
  349. }
  350.  
  351. }
  352. private void changePassword(string currpass, string newpass, string confirmpass)
  353. {
  354. DialogResult dr = MessageBox.Show("Are you sure you want to change your password?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  355. if (dr == DialogResult.Yes)
  356. {
  357. if (currpass == "" || newpass == "" || confirmpass == "")
  358. {
  359. MessageBox.Show("Fields Can\'t be blank!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  360. }
  361. else
  362. {
  363. if (currpass == GlobalVar.user_pass)
  364. {
  365. if (newpass == confirmpass)
  366. {
  367. if (dbhelper.openConnection())
  368. {
  369. string query = "UPDATE tblaccounts set pass_word=@newpass WHERE AccountID=@id";
  370. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  371. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  372. cmd.Parameters.AddWithValue("newpass", MD5Hasher.GetMd5Hash(newpass));
  373. cmd.ExecuteNonQuery();
  374. dbhelper.CloseConnection();
  375.  
  376. }
  377. MessageBox.Show("You have successfully changed your password!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
  378. GlobalVar.user_pass = newpass;
  379. txtconfirmpass.Text = "";
  380. txtcurrpass.Text = "";
  381. txtnewpass.Text = "";
  382. }
  383. else
  384. {
  385. MessageBox.Show("New password mismatched! Please Retype your new password!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  386. }
  387. }
  388. else
  389. {
  390. MessageBox.Show("Current password mismatched! Please Retype your current password!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  391. }
  392. }
  393. }
  394.  
  395.  
  396. }
  397. private void loadBorrowedBooks()
  398. {
  399. if (dbhelper.openConnection())
  400. {
  401. string query = "SELECT Concat(s.last_name, ',', s.first_name, ' ', s.middle_name ) as 'Borrower', BookName, Issuer, Quantity, DateBorrowed, DueDate FROM borrowedBooksView INNER JOIN tblstudents as s ON AccountID = BorrowerID";
  402. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  403. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  404. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  405. borrowedBooks = new DataTable();
  406. da.Fill(borrowedBooks);
  407. dgvborrowed.DataSource = borrowedBooks;
  408. dbhelper.CloseConnection();
  409. }
  410. if (dbhelper.openConnection())
  411. {
  412. int noOfDistinctBook = 0;
  413. string query = "Select COUNT(*) as NoOfBooks from borrowedBooksView";
  414. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  415. MySqlDataReader reader = cmd.ExecuteReader();
  416. while (reader.Read())
  417. {
  418. noOfDistinctBook = int.Parse(reader["NoOfBooks"].ToString());
  419. }
  420. dbhelper.CloseConnection();
  421. lblBookCountBorrowed.Text = "Book Count: " + noOfDistinctBook.ToString();
  422. }
  423. }
  424. private void changeUsername(string newusername)
  425. {
  426. DialogResult dr = MessageBox.Show("Are you sure you want to change your username?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  427. if (dr == DialogResult.Yes)
  428. {
  429. int count = 0;
  430. if (dbhelper.openConnection())
  431. {
  432. string query = "Select COUNT(*) as Total from tblaccounts WHERE user_name=@user";
  433. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  434. cmd.Parameters.AddWithValue("user", newusername);
  435. MySqlDataReader reader = cmd.ExecuteReader();
  436. while (reader.Read())
  437. {
  438. count = int.Parse(reader["Total"].ToString());
  439. }
  440. dbhelper.CloseConnection();
  441. }
  442. MessageBox.Show(count.ToString());
  443. if (count == 0)
  444. {
  445. if (dbhelper.openConnection())
  446. {
  447. string query = "UPDATE tblaccounts set user_name=@newuser WHERE AccountID=@id";
  448. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  449. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  450. cmd.Parameters.AddWithValue("newuser", newusername);
  451. cmd.ExecuteNonQuery();
  452. dbhelper.CloseConnection();
  453.  
  454. }
  455. MessageBox.Show("You have successfully changed your username!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
  456. GlobalVar.user_logged = txtuser.Text;
  457. txtuser.Text = "";
  458. }
  459. else
  460. {
  461. MessageBox.Show("It seems that your preferred username is already taken!", "Taken", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  462. }
  463. }
  464. }
  465.  
  466. private void btnchangeusername_Click(object sender, EventArgs e)
  467. {
  468. changeUsername(txtuser.Text);
  469. }
  470.  
  471. private void btnpass_Click(object sender, EventArgs e)
  472. {
  473. changePassword(txtcurrpass.Text, txtnewpass.Text, txtconfirmpass.Text);
  474. }
  475.  
  476. private void btnedit_Click(object sender, EventArgs e)
  477. {
  478. if (btnedit.Text == "Edit")
  479. {
  480. fname.ReadOnly = false;
  481. mname.ReadOnly = false;
  482. lname.ReadOnly = false;
  483.  
  484.  
  485. btnedit.Text = "Save";
  486. }
  487. else if (btnedit.Text == "Save")
  488. {
  489.  
  490. if (mname.Text == "" || fname.Text == "" || lname.Text == "")
  491. {
  492. MessageBox.Show("Please fill up necessary fields!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  493. }
  494. else
  495. {
  496. if (dbhelper.openConnection())
  497. {
  498. string query = "UPDATE tbllibrarian set first_name=@fname, middle_name=@mname, last_name=@lname WHERE AccountID=@id";
  499. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  500. cmd.Parameters.AddWithValue("fname", fname.Text);
  501. cmd.Parameters.AddWithValue("mname", mname.Text);
  502. cmd.Parameters.AddWithValue("lname", lname.Text);
  503. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  504. cmd.ExecuteNonQuery();
  505. dbhelper.CloseConnection();
  506.  
  507. }
  508. MessageBox.Show("Account information updated!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  509.  
  510. fname.ReadOnly = true;
  511. mname.ReadOnly = true;
  512. lname.ReadOnly = true;
  513. btnedit.Text = "Edit";
  514.  
  515. getName();
  516. }
  517. }
  518. }
  519.  
  520. private void btnAddacount_Click(object sender, EventArgs e)
  521. {
  522. if (txtapassword.Text == txtaconfirm.Text)
  523. {
  524. if (dbhelper.openConnection())
  525. {
  526. if(cbotype.Text == "student")
  527. {
  528. string query = "Insert into tblaccounts(user_name, pass_word, Account_Type) VALUES (@user, @pass, @type);Insert into tblstudents(AccountID, first_name, middle_name, last_name, grade, section) VALUES (LAST_INSERT_ID(), @fname , @mname, @lname , @grade , @section )";
  529. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  530. cmd.Parameters.AddWithValue("user", txtausername.Text);
  531. cmd.Parameters.AddWithValue("pass", MD5Hasher.GetMd5Hash(txtapassword.Text));
  532. cmd.Parameters.AddWithValue("type", cbotype.Text);
  533. cmd.Parameters.AddWithValue("fname", txtafname.Text);
  534. cmd.Parameters.AddWithValue("mname", txtamname.Text);
  535. cmd.Parameters.AddWithValue("lname", txtaLname.Text);
  536. cmd.Parameters.AddWithValue("grade", cboagrade.Text);
  537. cmd.Parameters.AddWithValue("section", txtasection.Text);
  538. cmd.ExecuteNonQuery();
  539.  
  540. }
  541. else if(cbotype.Text == "librarian")
  542. {
  543. string query = "Insert into tblaccounts(user_name, pass_word, Account_Type) VALUES (@user, @pass, @type);Insert into tbllibrarian(AccountID, first_name, middle_name, last_name) VALUES (LAST_INSERT_ID(), @fname , @mname, @lname)";
  544. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  545. cmd.Parameters.AddWithValue("user", txtausername.Text);
  546. cmd.Parameters.AddWithValue("pass", MD5Hasher.GetMd5Hash(txtapassword.Text));
  547. cmd.Parameters.AddWithValue("type", cbotype.Text);
  548. cmd.Parameters.AddWithValue("fname", txtafname.Text);
  549. cmd.Parameters.AddWithValue("mname", txtamname.Text);
  550. cmd.Parameters.AddWithValue("lname", txtaLname.Text);
  551. cmd.ExecuteNonQuery();
  552. }
  553.  
  554.  
  555. string query2 = "INSERT INTO tblanswers(AccountID, QuestionID, Answer) VALUES (LAST_INSERT_ID(), (SELECT tblsecques.QuestionID FROM tblsecques WHERE Question=CONVERT(@question USING utf8)), @answer)";
  556. MySqlCommand cmd2 = new MySqlCommand(query2, dbhelper.getConnection());
  557. cmd2.Parameters.AddWithValue("question", cboasecurity.Text);
  558. cmd2.Parameters.AddWithValue("answer", txtaanswer.Text);
  559. cmd2.ExecuteNonQuery();
  560. }
  561.  
  562. dbhelper.CloseConnection();
  563. MessageBox.Show("Added new Account!");
  564.  
  565. dgvaedAccount.Rows.Clear();
  566. loadaedAccounts(cbosearchtype.Text, "");
  567. clearFields();
  568.  
  569. }
  570. else
  571. {
  572. MessageBox.Show("Password do not match!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  573. }
  574. }
  575.  
  576.  
  577. private void loadsecurity()
  578. {
  579. if (dbhelper.openConnection())
  580. {
  581. string query = "SELECT * from tblsecques";
  582. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  583. MySqlDataReader reader = cmd.ExecuteReader();
  584. while (reader.Read())
  585. {
  586. cboasecurity.Items.Add(reader.GetString(1));
  587. }
  588. }
  589. dbhelper.CloseConnection();
  590.  
  591. }
  592.  
  593. private void cbotype_SelectedIndexChanged(object sender, EventArgs e)
  594. {
  595. if(editing == false)
  596. {
  597. if (cbotype.Text == "librarian")
  598. {
  599. cboagrade.Text = "";
  600. txtasection.Text = "";
  601. cboagrade.Enabled = false;
  602. txtasection.ReadOnly = true;
  603. }
  604. else if (cbotype.Text == "student")
  605. {
  606. cboagrade.SelectedIndex = 0;
  607. txtasection.Text = tempsection;
  608. cboagrade.Enabled = true;
  609. txtasection.ReadOnly = false;
  610. }
  611. }
  612. else
  613. {
  614. if (cbotype.Text == "librarian")
  615. {
  616. cboagrade.Text = "";
  617. txtasection.Text = "";
  618. cboagrade.Enabled = false;
  619. txtasection.ReadOnly = true;
  620.  
  621. txtausername.Text = "Owner of account can only change username!";
  622. txtapassword.Text = "Owner of account can only change password!";
  623. }
  624. else if (cbotype.Text == "student")
  625. {
  626. cboagrade.Text = tempgrade;
  627. txtasection.Text = tempsection;
  628.  
  629. cboagrade.Enabled = true;
  630. txtasection.ReadOnly = false;
  631. }
  632. }
  633. }
  634.  
  635.  
  636. private void loadaedAccounts(string type, string search)
  637. {
  638. dgvaedAccount.ColumnCount = 2;
  639. dgvaedAccount.Columns[0].Name = "#";
  640. dgvaedAccount.Columns[0].Width = 60;
  641. dgvaedAccount.Columns[1].Name = "FullName";
  642.  
  643.  
  644. if (type == "student")
  645. {
  646. if (dbhelper.openConnection())
  647. {
  648. string query = "";
  649. if(search != "")
  650. {
  651. query = string.Format("SELECT * FROM studentsView WHERE FullName Like '%{0}%'", search);
  652. }
  653. else
  654. {
  655. query = "SELECT * FROM studentsView";
  656. }
  657. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  658. MySqlDataReader reader = cmd.ExecuteReader();
  659. while (reader.Read())
  660. {
  661. string[] info = { reader["AccountID"].ToString(), reader["FullName"].ToString() };
  662. dgvaedAccount.Rows.Add(info);
  663.  
  664.  
  665. }
  666. }
  667. dbhelper.CloseConnection();
  668.  
  669. }
  670. else if (type == "librarian")
  671. {
  672. if (dbhelper.openConnection())
  673. {
  674. string query = "";
  675. if(search != "")
  676. {
  677. query = string.Format("SELECT * FROM librarianView WHERE Like '%{0}%'", search);
  678. }
  679. else
  680. {
  681. query = "SELECT * FROM librarianView";
  682. }
  683.  
  684. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  685. MySqlDataReader reader = cmd.ExecuteReader();
  686. while (reader.Read())
  687. {
  688. string[] info = { reader["AccountID"].ToString(), reader["FullName"].ToString() };
  689. dgvaedAccount.Rows.Add(info);
  690.  
  691.  
  692. }
  693. }
  694. dbhelper.CloseConnection();
  695. }
  696. DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
  697. dgvaedAccount.Columns.Add(btn);
  698. btn.HeaderText = "Edit Data";
  699. btn.Text = "Edit";
  700. btn.Name = "btn";
  701. btn.UseColumnTextForButtonValue = true;
  702.  
  703. DataGridViewButtonColumn btndel = new DataGridViewButtonColumn();
  704. dgvaedAccount.Columns.Add(btndel);
  705. btndel.HeaderText = "Delete Account";
  706. btndel.Text = "Delete";
  707. btndel.Name = "btndel";
  708. btndel.UseColumnTextForButtonValue = true;
  709.  
  710. }
  711.  
  712. private void cbosearchtype_SelectedIndexChanged(object sender, EventArgs e)
  713. {
  714. dgvaedAccount.Rows.Clear();
  715.  
  716. loadaedAccounts(cbosearchtype.Text, "");
  717. }
  718.  
  719. private void dgvaedAccount_CellContentClick(object sender, DataGridViewCellEventArgs e)
  720. {
  721.  
  722. if (e.RowIndex >= 0 && e.RowIndex != dgvaedAccount.RowCount -1)
  723. {
  724.  
  725. if(e.ColumnIndex == 2)
  726. {
  727. editing = true;
  728. if (editing)
  729. {
  730. btnCancel.Enabled = true;
  731. btnSave.Enabled = true;
  732. btnAddacount.Enabled = false;
  733. }
  734.  
  735. DataGridViewRow row = this.dgvaedAccount.Rows[e.RowIndex];
  736. accid = row.Cells["#"].Value.ToString();
  737.  
  738. txtausername.ReadOnly = true;
  739. txtapassword.ReadOnly = true;
  740. txtaconfirm.ReadOnly = true;
  741.  
  742. //display its data in
  743. if (cbosearchtype.Text == "student")
  744. {
  745. if (dbhelper.openConnection())
  746. {
  747. string query = "SELECT * FROM tblstudents WHERE AccountID=@id";
  748. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  749. cmd.Parameters.AddWithValue("id", accid);
  750. MySqlDataReader reader = cmd.ExecuteReader();
  751. while (reader.Read())
  752. {
  753. cbotype.Text = "student";
  754. txtafname.Text = reader["first_name"].ToString();
  755. txtamname.Text = reader["middle_name"].ToString();
  756. txtaLname.Text = reader["last_name"].ToString();
  757. cboagrade.Text = reader["grade"].ToString();
  758. txtasection.Text = reader["section"].ToString();
  759. txtausername.Text = "Owner of account can only change username!";
  760. txtapassword.Text = "Owner of account can only change password!";
  761. txtaconfirm.Text = "-----------";
  762.  
  763. tempsection = txtasection.Text;
  764. tempgrade = cboagrade.Text;
  765. }
  766. }
  767. dbhelper.CloseConnection();
  768.  
  769. //get security question and answer
  770. if (dbhelper.openConnection())
  771. {
  772. string query = "SELECT * FROM answersView WHERE AccountID=@id";
  773. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  774. cmd.Parameters.AddWithValue("id", accid);
  775. MySqlDataReader reader = cmd.ExecuteReader();
  776. while (reader.Read())
  777. {
  778. cboasecurity.Text = reader["Question"].ToString();
  779. txtaanswer.Text = reader["Answer"].ToString();
  780.  
  781. }
  782. }
  783. dbhelper.CloseConnection();
  784.  
  785. }
  786. else if (cbosearchtype.Text == "librarian")
  787. {
  788.  
  789. }
  790. }else if (e.ColumnIndex == 3)
  791. {
  792. DataGridViewRow row = this.dgvaedAccount.Rows[e.RowIndex];
  793. accid = row.Cells["#"].Value.ToString();
  794. string fullname = row.Cells["FullName"].Value.ToString();
  795. int count = 0;
  796.  
  797. DialogResult dr = MessageBox.Show("Are you sure you want to delete account of " + fullname + "?","Delete Account", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  798. if(dr == DialogResult.Yes)
  799. {
  800. if (dbhelper.openConnection())
  801. {
  802. string query = "Select Count(*) as Total From tblborrowed where BorrowerID=@id";
  803. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  804. cmd.Parameters.AddWithValue("id", accid);
  805. MySqlDataReader reader = cmd.ExecuteReader();
  806.  
  807. while (reader.Read())
  808. {
  809. count = int.Parse(reader["Total"].ToString());
  810. }
  811. dbhelper.CloseConnection();
  812. }
  813. if(count > 0)
  814. {
  815. MessageBox.Show("Account has loaned books!");
  816. }
  817. else if(count == 0)
  818. {
  819. if(cbosearchtype.Text == "librarian")
  820. {
  821. if (dbhelper.openConnection())
  822. {
  823. string query = "Delete from tblaccounts where AccountID=@id;Delete From tblibrarian Where AccountID=@id; Delete From tblanswers Where AccountID=@id; Delete from tblloghistory where AccountID=@id";
  824. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  825. cmd.Parameters.AddWithValue("id", accid);
  826. cmd.ExecuteNonQuery();
  827. }
  828. }
  829. else if (cbosearchtype.Text == "student")
  830. {
  831. if (dbhelper.openConnection())
  832. {
  833. string query = "Delete from tblaccounts where AccountID=@id;Delete From tblstudents Where AccountID=@id; Delete From tblanswers Where AccountID=@id; Delete from tblloghistory where AccountID=@id";
  834. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  835. cmd.Parameters.AddWithValue("id", accid);
  836. cmd.ExecuteNonQuery();
  837. }
  838. }
  839. dbhelper.CloseConnection();
  840. dgvaedAccount.Rows.Clear();
  841. loadaedAccounts(cbosearchtype.Text, "");
  842. MessageBox.Show("Deleted Account!", "Sucess", MessageBoxButtons.OK, MessageBoxIcon.Information);
  843. }
  844. }
  845. }
  846. }
  847. }
  848.  
  849. private void btnCancel_Click(object sender, EventArgs e)
  850. {
  851. editing = false;
  852. if (editing == false)
  853. {
  854.  
  855. clearFields();
  856. }
  857. }
  858.  
  859. private void clearFields()
  860. {
  861. btnCancel.Enabled = false;
  862. btnSave.Enabled = false;
  863. btnAddacount.Enabled = true;
  864.  
  865. txtausername.ReadOnly = false;
  866. txtapassword.ReadOnly = false;
  867. txtaconfirm.ReadOnly = false;
  868. accid = "";
  869.  
  870. cbotype.Text = "student";
  871. txtafname.Text = "";
  872. txtamname.Text = "";
  873. txtaLname.Text = "";
  874. cboagrade.Text = "7";
  875. txtasection.Text = "";
  876. txtausername.Text = "";
  877. txtapassword.Text = "";
  878. txtaconfirm.Text = "";
  879.  
  880. txtaanswer.Text = "";
  881. cboasecurity.SelectedIndex = 0;
  882.  
  883. tempgrade = "";
  884. tempsection = "";
  885. }
  886.  
  887. private void txtasearch_TextChanged(object sender, EventArgs e)
  888. {
  889. dgvaedAccount.Rows.Clear();
  890. loadaedAccounts(cbosearchtype.Text, txtasearch.Text);
  891. }
  892.  
  893. private void txtafname_KeyPress(object sender, KeyPressEventArgs e)
  894. {
  895. if (!Char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !Char.IsWhiteSpace(e.KeyChar)) //text only input and spaces
  896. e.Handled = true;
  897. base.OnKeyPress(e);
  898. }
  899.  
  900. private void adstxtsearch_TextChanged(object sender, EventArgs e)
  901. {
  902. try
  903. {
  904. DataView DV = new DataView(books);
  905. DV.RowFilter = string.Format(adscboSearchBy.Text + " LIKE '%{0}%'", adstxtsearch.Text);
  906. dgvBooks.DataSource = DV;
  907. }
  908. catch (Exception ex)
  909. {
  910. MessageBox.Show("Invalid Input!" + ex.ToString());
  911. }
  912. }
  913.  
  914. //===========Add new book
  915. private void addEditButton()
  916. {
  917. DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
  918. dgvAddBook.Columns.Add(btn);
  919. btn.HeaderText = "Edit Data";
  920. btn.Text = "Edit";
  921. btn.Name = "btn";
  922. btn.UseColumnTextForButtonValue = true;
  923. }
  924.  
  925. private void addtxtsearch_TextChanged(object sender, EventArgs e)
  926. {
  927. try
  928. {
  929. DataView DV = new DataView(books);
  930. DV.RowFilter = string.Format(addcbosearchby.Text + " LIKE '%{0}%'", addtxtsearch.Text);
  931. dgvAddBook.DataSource = DV;
  932. }
  933. catch (Exception ex)
  934. {
  935. MessageBox.Show("Invalid Input!" + ex.ToString());
  936. }
  937. }
  938. }
  939. }
  940. frmStart.cs
  941. using System;
  942. using System.Collections.Generic;
  943. using System.ComponentModel;
  944. using System.Data;
  945. using System.Drawing;
  946. using System.Linq;
  947. using System.Text;
  948. using System.Windows.Forms;
  949. using MySql.Data.MySqlClient;
  950. namespace LibrarySystem
  951. {
  952. public partial class frmstart : Form
  953. {
  954. DatabaseHelper dbhelper;
  955. int trials = 3;
  956. public frmstart()
  957. {
  958. InitializeComponent();
  959.  
  960. Connection.setServer("127.0.0.1");
  961. Connection.setDatabase("dblibrary");
  962. Connection.setUid("root");
  963. Connection.setPassword("");
  964.  
  965. dbhelper = new DatabaseHelper(Connection.getConnectionStr());
  966.  
  967. txttrials.Text = "Trials Left: " + trials.ToString();
  968. }
  969.  
  970. private void btnLogin_Click(object sender, EventArgs e)
  971. {
  972. if (txtusername.Text == "" || txtpassword.Text == "")
  973. {
  974.  
  975. MessageBox.Show("Username / Password can't be blank!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
  976. }
  977. else
  978. {
  979. if (trials == 0 || trials <= 0)
  980. {
  981. MessageBox.Show("You have used all your trials!, Please restart the application!", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  982. }
  983. else
  984. {
  985. GlobalVar.user_logged = txtusername.Text;
  986.  
  987. GlobalVar.user_pass = txtpassword.Text;
  988. if (dbhelper.openConnection())
  989. {
  990. try
  991. {
  992. string query = "SELECT Account_Type, AccountID, Account_Type FROM tblaccounts WHERE user_name = @user AND pass_word = @pass";
  993. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  994. cmd.Parameters.AddWithValue("@user", txtusername.Text);
  995. cmd.Parameters.AddWithValue("@pass", MD5Hasher.GetMd5Hash(txtpassword.Text));
  996. MySqlDataReader reader = cmd.ExecuteReader();
  997. int count = 0;
  998.  
  999. while (reader.Read())
  1000. {
  1001. count++;
  1002. GlobalVar.user_type = reader["Account_Type"].ToString();
  1003. GlobalVar.user_id = reader["AccountID"].ToString();
  1004. }
  1005.  
  1006. if (count == 1)
  1007. {
  1008. MessageBox.Show("Login Succesful!");
  1009. trials = 3;
  1010. txttrials.Text = "Trials Left: " + trials.ToString();
  1011. if (GlobalVar.user_type == "student")
  1012. {
  1013. frmStudent std = new frmStudent();
  1014. std.ShowDialog();
  1015. }
  1016. else if (GlobalVar.user_type == "librarian")
  1017. {
  1018. frmLibrarian lib = new frmLibrarian();
  1019. lib.ShowDialog();
  1020.  
  1021. }
  1022.  
  1023. txtusername.Text = "";
  1024. txtpassword.Text = "";
  1025. }
  1026. else if (count == 0)
  1027. {
  1028. MessageBox.Show("Login Failed!");
  1029. trials--;
  1030. txttrials.Text = "Trials Left: " + trials.ToString();
  1031.  
  1032. }
  1033. }
  1034. catch (MySqlException ex)
  1035. {
  1036. MessageBox.Show(ex.Message);
  1037. }
  1038. }
  1039.  
  1040. dbhelper.CloseConnection();
  1041.  
  1042. }
  1043. }
  1044. }
  1045.  
  1046. private void lkForgot_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  1047. {
  1048. frmForgot forg = new frmForgot();
  1049. forg.ShowDialog();
  1050. }
  1051. }
  1052. }
  1053. frmStudents.cs
  1054.  
  1055. using System;
  1056. using System.Collections.Generic;
  1057. using System.ComponentModel;
  1058. using System.Data;
  1059. using System.Drawing;
  1060. using System.Linq;
  1061. using System.Text;
  1062. using System.Windows.Forms;
  1063. using MySql.Data.MySqlClient;
  1064.  
  1065. namespace LibrarySystem
  1066. {
  1067. public partial class frmStudent : Form
  1068. {
  1069. DatabaseHelper dbhelper;
  1070. DataTable books;
  1071. DataTable borrowedBooks;
  1072. public frmStudent()
  1073. {
  1074. InitializeComponent();
  1075. Connection.setServer("127.0.0.1");
  1076. Connection.setDatabase("dblibrary");
  1077. Connection.setUid("root");
  1078. Connection.setPassword("");
  1079. dbhelper = new DatabaseHelper(Connection.getConnectionStr());
  1080. loadBooks();
  1081. loadBorrowedBooks();
  1082. getName();
  1083. loadAccountInfo();
  1084. }
  1085.  
  1086. private void loadBooks()
  1087. {
  1088.  
  1089. if (dbhelper.openConnection())
  1090. {
  1091. string query = "SELECT * FROM tblbooks";
  1092. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1093. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  1094. books = new DataTable();
  1095. da.Fill(books);
  1096. DgvBooks.DataSource = books;
  1097. dbhelper.CloseConnection();
  1098. }
  1099. //count number of books in sink
  1100. if (dbhelper.openConnection())
  1101. {
  1102. int noOfDistinctBook = 0;
  1103. string query = "Select COUNT(*) as NoOfBooks from tblbooks";
  1104. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1105. MySqlDataReader reader = cmd.ExecuteReader();
  1106. while (reader.Read())
  1107. {
  1108. noOfDistinctBook = int.Parse(reader["NoOfBooks"].ToString());
  1109. }
  1110. dbhelper.CloseConnection();
  1111. lblbookcount.Text = "Book Count: " + noOfDistinctBook.ToString();
  1112. }
  1113. }
  1114.  
  1115. private void loadBorrowedBooks()
  1116. {
  1117.  
  1118. if (dbhelper.openConnection())
  1119. {
  1120. string query = "SELECT BookName, Issuer, Quantity, DateBorrowed, DueDate FROM borrowedBooksView WHERE BorrowerID=@id";
  1121. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1122. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1123. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  1124. borrowedBooks = new DataTable();
  1125. da.Fill(borrowedBooks);
  1126. dgvBorrowed.DataSource = borrowedBooks;
  1127. dbhelper.CloseConnection();
  1128. }
  1129. //count borrowed books
  1130. if (dbhelper.openConnection())
  1131. {
  1132. int noOfDistinctBook = 0;
  1133. string query = "Select COUNT(*) as NoOfBooks from borrowedBooksView WHERE BorrowerID=@id";
  1134. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1135. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1136. MySqlDataReader reader = cmd.ExecuteReader();
  1137. while (reader.Read())
  1138. {
  1139. noOfDistinctBook = int.Parse(reader["NoOfBooks"].ToString());
  1140. }
  1141. dbhelper.CloseConnection();
  1142. lblBookCountBorrowed.Text = "Book Count: " + noOfDistinctBook.ToString();
  1143. }
  1144. }
  1145.  
  1146. private void getName()
  1147. {
  1148. if (dbhelper.openConnection())
  1149. {
  1150. string fullname = "";
  1151. string query = "Select CONCAT(last_name, ',' , first_name, ' ', middle_name) as FullName from tblstudents WHERE AccountID=@id";
  1152. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1153. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1154. MySqlDataReader reader = cmd.ExecuteReader();
  1155. while (reader.Read())
  1156. {
  1157. fullname = reader["Fullname"].ToString();
  1158. }
  1159. dbhelper.CloseConnection();
  1160. lblWelcome.Text = "Welcome: " + fullname;
  1161. }
  1162. }
  1163.  
  1164. private void btnLogout_Click(object sender, EventArgs e)
  1165. {
  1166. this.Close();
  1167. }
  1168.  
  1169. private void frmStudent_FormClosing(object sender, FormClosingEventArgs e)
  1170. {
  1171. DialogResult dr = MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1172. if(dr == DialogResult.No)
  1173. {
  1174. e.Cancel = true;
  1175.  
  1176. }
  1177. }
  1178. private void loadAccountInfo()
  1179. {
  1180. try
  1181. {
  1182. if (dbhelper.openConnection())
  1183. {
  1184. string query = "SELECT * FROM tblstudents WHERE AccountID= @id";
  1185. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1186. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1187. MySqlDataReader reader = cmd.ExecuteReader();
  1188.  
  1189. while (reader.Read())
  1190. {
  1191. fname.Text = reader["first_name"].ToString();
  1192. mname.Text = reader["middle_name"].ToString();
  1193. lname.Text = reader["last_name"].ToString();
  1194. grade.Text = reader["grade"].ToString();
  1195. section.Text = reader["section"].ToString();
  1196. }
  1197. dbhelper.CloseConnection();
  1198. }
  1199.  
  1200. }
  1201. catch (Exception ex)
  1202. {
  1203. MessageBox.Show(ex.ToString());
  1204. }
  1205.  
  1206. }
  1207. private void changePassword(string currpass, string newpass, string confirmpass)
  1208. {
  1209. DialogResult dr = MessageBox.Show("Are you sure you want to change your password?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1210. if (dr == DialogResult.Yes)
  1211. {
  1212. if (currpass == "" || newpass == "" || confirmpass == "")
  1213. {
  1214. MessageBox.Show("Fields Can\'t be blank!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1215. }
  1216. else
  1217. {
  1218. if (currpass == GlobalVar.user_pass)
  1219. {
  1220. if (newpass == confirmpass)
  1221. {
  1222. if (dbhelper.openConnection())
  1223. {
  1224. string query = "UPDATE tblaccounts set pass_word=@newpass WHERE AccountID=@id";
  1225. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1226. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1227. cmd.Parameters.AddWithValue("newpass", MD5Hasher.GetMd5Hash(newpass));
  1228. cmd.ExecuteNonQuery();
  1229. dbhelper.CloseConnection();
  1230.  
  1231. }
  1232. MessageBox.Show("You have successfully changed your password!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1233. GlobalVar.user_pass = newpass;
  1234. txtconfirmpass.Text = "";
  1235. txtcurrpass.Text = "";
  1236. txtnewpass.Text = "";
  1237. }
  1238. else
  1239. {
  1240. MessageBox.Show("New password mismatched! Please Retype your new password!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1241. }
  1242. }
  1243. else
  1244. {
  1245. MessageBox.Show("Current password mismatched! Please Retype your current password!", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1246. }
  1247. }
  1248. }
  1249.  
  1250.  
  1251. }
  1252.  
  1253. private void changeUsername(string newusername)
  1254. {
  1255. DialogResult dr = MessageBox.Show("Are you sure you want to change your username?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  1256. if (dr == DialogResult.Yes)
  1257. {
  1258. //check if there is duplicate
  1259. int count = 0;
  1260. if (dbhelper.openConnection())
  1261. {
  1262. string query = "Select COUNT(*) as Total from tblaccounts WHERE user_name=@user";
  1263. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1264. cmd.Parameters.AddWithValue("user", newusername);
  1265. MySqlDataReader reader = cmd.ExecuteReader();
  1266. while (reader.Read())
  1267. {
  1268. count = int.Parse(reader["Total"].ToString());
  1269. }
  1270. dbhelper.CloseConnection();
  1271. }
  1272. MessageBox.Show(count.ToString());
  1273. if (count == 0)
  1274. {
  1275. if (dbhelper.openConnection())
  1276. {
  1277. string query = "UPDATE tblaccounts set user_name=@newuser WHERE AccountID=@id";
  1278. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1279. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1280. cmd.Parameters.AddWithValue("newuser", newusername);
  1281. cmd.ExecuteNonQuery();
  1282. dbhelper.CloseConnection();
  1283.  
  1284. }
  1285. MessageBox.Show("You have successfully changed your username!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1286. GlobalVar.user_logged = txtnewuser.Text;
  1287. txtnewuser.Text = "";
  1288. }
  1289. else
  1290. {
  1291. MessageBox.Show("It seems that your preferred username is already taken!", "Taken", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1292. }
  1293. }
  1294. }
  1295.  
  1296. private void btnChangeUsername_Click(object sender, EventArgs e)
  1297. {
  1298. changeUsername(txtnewuser.Text);
  1299. }
  1300.  
  1301. private void btnchangepass_Click(object sender, EventArgs e)
  1302. {
  1303. changePassword(txtcurrpass.Text, txtnewpass.Text, txtconfirmpass.Text);
  1304. }
  1305.  
  1306. private void btnedit_Click(object sender, EventArgs e)
  1307. {
  1308. if(btnedit.Text == "Edit")
  1309. {
  1310. //enable text fields for modification
  1311. fname.ReadOnly = false;
  1312. mname.ReadOnly = false;
  1313. lname.ReadOnly = false;
  1314. grade.ReadOnly = false;
  1315. section.ReadOnly = false;
  1316.  
  1317. btnedit.Text = "Save";
  1318. }
  1319. else if(btnedit.Text == "Save")
  1320. {
  1321. //change account info
  1322.  
  1323.  
  1324.  
  1325. if(mname.Text == "" || fname.Text =="" || lname.Text == "" || grade.Text == "" || section.Text == "")
  1326. {
  1327. MessageBox.Show("Please fill up necessary fields!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1328. }
  1329. else
  1330. {
  1331. if(int.Parse(grade.Text) > 12 || int.Parse(grade.Text) < 7)
  1332. {
  1333. MessageBox.Show("Invalid Grade!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1334. }
  1335. else
  1336. {
  1337. if (dbhelper.openConnection())
  1338. {
  1339. string query = "UPDATE tblstudents set first_name=@fname, middle_name=@mname, last_name=@lname, grade=@grd , section=@section WHERE AccountID=@id";
  1340. MySqlCommand cmd = new MySqlCommand(query, dbhelper.getConnection());
  1341. cmd.Parameters.AddWithValue("fname", fname.Text);
  1342. cmd.Parameters.AddWithValue("mname", mname.Text);
  1343. cmd.Parameters.AddWithValue("lname", lname.Text);
  1344. cmd.Parameters.AddWithValue("grd", grade.Text);
  1345. cmd.Parameters.AddWithValue("section", section.Text);
  1346. cmd.Parameters.AddWithValue("id", GlobalVar.user_id);
  1347. cmd.ExecuteNonQuery();
  1348. dbhelper.CloseConnection();
  1349.  
  1350. }
  1351. MessageBox.Show("Account information updated!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  1352.  
  1353. fname.ReadOnly = true;
  1354. mname.ReadOnly = true;
  1355. lname.ReadOnly = true;
  1356. grade.ReadOnly = true;
  1357. section.ReadOnly = true;
  1358. btnedit.Text = "Edit";
  1359.  
  1360. getName();
  1361. }
  1362. }
  1363.  
  1364. }
  1365. }
  1366. }
  1367.  
  1368. }
  1369.  
  1370. GlobalVar.cs
  1371. using System;
  1372. using System.Collections.Generic;
  1373. using System.Linq;
  1374. using System.Text;
  1375.  
  1376. namespace LibrarySystem
  1377. {
  1378. class GlobalVar
  1379. {
  1380. public static String user_logged = String.Empty;
  1381. public static String user_type = "";
  1382. public static String user_pass = "";
  1383. public static String user_id = "";
  1384. }
  1385. }
  1386.  
  1387. MD5Hasher.cs
  1388. using System;
  1389. using System.Collections.Generic;
  1390. using System.Linq;
  1391. using System.Text;
  1392. using System.Security.Cryptography;
  1393. namespace LibrarySystem
  1394. {
  1395. class MD5Hasher
  1396. {
  1397. public static string GetMd5Hash(string input)
  1398. {
  1399. MD5 md5Hash = MD5.Create();
  1400. // Convert the input string to a byte array and compute the hash.
  1401. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  1402.  
  1403. // Create a new Stringbuilder to collect the bytes
  1404. // and create a string.
  1405. StringBuilder sBuilder = new StringBuilder();
  1406.  
  1407. // Loop through each byte of the hashed data
  1408. // and format each one as a hexadecimal string.
  1409. for (int i = 0; i < data.Length; i++)
  1410. {
  1411. sBuilder.Append(data[i].ToString("x2"));
  1412. }
  1413.  
  1414. // Return the hexadecimal string.
  1415. return sBuilder.ToString();
  1416. }
  1417.  
  1418. }
  1419. }
  1420.  
  1421. Program.cs
  1422. using System;
  1423. using System.Collections.Generic;
  1424. using System.Linq;
  1425. using System.Windows.Forms;
  1426.  
  1427. namespace LibrarySystem
  1428. {
  1429. static class Program
  1430. {
  1431. /// <summary>
  1432. /// The main entry point for the application.
  1433. /// </summary>
  1434. [STAThread]
  1435. static void Main()
  1436. {
  1437. Application.EnableVisualStyles();
  1438. Application.SetCompatibleTextRenderingDefault(false);
  1439. Application.Run(new frmstart());
  1440. }
  1441. }
  1442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement