Advertisement
Guest User

WAWILORD DATABASE 2/12/2016 2

a guest
Feb 11th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1.  
  2. WITH SEARCH FUNCTION
  3.  
  4.  
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. using System.Data.SqlClient;
  15.  
  16. namespace Macarse_Grocery
  17. {
  18.  
  19. public partial class Form1 : Form
  20. {
  21. SqlConnection con = new SqlConnection
  22. (
  23. @"server = localhost\SQLEXPRESS; Initial Catalog = Macarse_Grocery; Integrated Security = true;"
  24. );
  25.  
  26. SqlCommand cmd;
  27. SqlDataReader rdr;
  28.  
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. }
  33.  
  34. private void loadCustomer()
  35. {
  36. dataCustomer.Rows.Clear();
  37. con.Open();
  38. cmd = new SqlCommand("SELECT custID, custLname, custFname, custMI FROM Customer;", con);
  39.  
  40. rdr = cmd.ExecuteReader();
  41.  
  42. while (rdr.Read())
  43. {
  44. dataCustomer.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString(), rdr[3].ToString());
  45. }
  46.  
  47. con.Close();
  48. }
  49.  
  50.  
  51.  
  52. private void loadProducts()
  53. {
  54. dataProducts.Rows.Clear();
  55. con.Open();
  56. cmd = new SqlCommand("SELECT prodID, prodName, prodPrice FROM Products;", con);
  57.  
  58. rdr = cmd.ExecuteReader();
  59.  
  60. while (rdr.Read())
  61. {
  62. dataProducts.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString());
  63. }
  64.  
  65. con.Close();
  66. }
  67.  
  68.  
  69. private void tabPage2_Click(object sender, EventArgs e)
  70. {
  71.  
  72. }
  73.  
  74. private void Form1_Load(object sender, EventArgs e)
  75. {
  76. loadCustomer();
  77. loadProducts();
  78. loadPurchase();
  79. }
  80.  
  81. private void btnPurchase_Click(object sender, EventArgs e)
  82. {
  83. if (dataCustomer.SelectedRows.Count != 0 && dataProducts.SelectedRows.Count != 0)
  84. {
  85.  
  86. con.Open();
  87. cmd = new SqlCommand(@"INSERT INTO Purchase
  88. (custID, prodID, DatePurchased)
  89. VALUES('" + txtcustID.Text + "','" + txtprodID.Text + "','" + DateTime.Now.ToString() + "');", con);
  90.  
  91. cmd.ExecuteNonQuery();
  92. con.Close();
  93. MessageBox.Show("Purchase Succesfully!");
  94. txtcustID.Clear();
  95. txtprodID.Clear();
  96. loadPurchase();
  97. }
  98. }
  99.  
  100. private void loadPurchase()
  101. {
  102. dataPurchase.Rows.Clear();
  103. con.Open();
  104. cmd = new SqlCommand(@"SELECT Purchase.PurchaseID AS BID,
  105. Customer.custFname + ' ' + Customer.custMi + ' ' + Customer.custLname AS
  106. 'Customer', Products.prodName + ' priced at ' + cast(Products.prodPrice AS varchar) AS 'Product' FROM Products
  107. INNER JOIN Purchase
  108. ON Products.prodID = Purchase.prodID
  109. INNER JOIN Customer
  110. ON Purchase.custID = Customer.custID", con);
  111. rdr = cmd.ExecuteReader();
  112.  
  113. while(rdr.Read())
  114. {
  115. dataPurchase.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString());
  116. }
  117. con.Close();
  118. }
  119.  
  120. private void txtcustID_TextChanged(object sender, EventArgs e)
  121. {
  122.  
  123. }
  124.  
  125. private void dataCustomer_SelectionChanged(object sender, EventArgs e)
  126. {
  127. if (dataCustomer.SelectedRows.Count != 0)
  128. {
  129. txtcustID.Text = dataCustomer.SelectedRows[0].Cells[0].Value.ToString();
  130. txtcustLname.Text = dataCustomer.SelectedRows[0].Cells[1].Value.ToString();
  131. txtcustFname.Text = dataCustomer.SelectedRows[0].Cells[2].Value.ToString();
  132. txtcustMI.Text = dataCustomer.SelectedRows[0].Cells[3].Value.ToString();
  133.  
  134. }
  135. }
  136.  
  137. private void txtprodID_TextChanged(object sender, EventArgs e)
  138. {
  139. if (dataProducts.SelectedRows.Count != 0)
  140. {
  141.  
  142.  
  143.  
  144. }
  145. }
  146.  
  147. private void dataProducts_SelectionChanged(object sender, EventArgs e)
  148. {
  149. if (dataProducts.SelectedRows.Count != 0)
  150. {
  151. txtprodID.Text = dataProducts.SelectedRows[0].Cells[0].Value.ToString();
  152. txtprodName.Text = dataProducts.SelectedRows[0].Cells[1].Value.ToString();
  153. txtprodPrice.Text = dataProducts.SelectedRows[0].Cells[2].Value.ToString();
  154.  
  155.  
  156. }
  157.  
  158. }
  159.  
  160.  
  161.  
  162. private void dataPurchase_CellContentClick(object sender, DataGridViewCellEventArgs e)
  163. {
  164.  
  165. }
  166.  
  167. private void label5_Click(object sender, EventArgs e)
  168. {
  169.  
  170. }
  171.  
  172. private void button1_Click(object sender, EventArgs e)
  173. {
  174. con.Open();
  175. cmd = new SqlCommand(@"INSERT INTO Customer
  176. (custLname, custFname, custMI)
  177. VALUES('" + txtcustLname.Text + "','" + txtcustFname.Text + "','"+txtcustMI.Text+ "')", con);
  178.  
  179. cmd.ExecuteNonQuery();
  180. con.Close();
  181.  
  182. MessageBox.Show("Customer Added!");
  183. loadCustomer();
  184. txtcustLname.Clear();
  185. txtcustFname.Clear();
  186. txtcustMI.Clear();
  187.  
  188. }
  189.  
  190. private void button2_Click(object sender, EventArgs e)
  191. {
  192. con.Open();
  193. cmd = new SqlCommand("UPDATE Customer SET custLname = '" + txtcustLname.Text + "', custFname = '" + txtcustFname.Text + "', custMI = '" +txtcustMI.Text+ "' WHERE custID = '" + txtcustID.Text
  194. + "';", con); // same shit above, but its update now
  195. cmd.ExecuteNonQuery();
  196. con.Close();
  197. MessageBox.Show("Update Succesful");
  198. loadCustomer(); //received the new input and just refresh again for it to show.
  199. txtcustLname.Clear();
  200. txtcustFname.Clear();
  201. txtcustMI.Clear();
  202. txtcustID.Clear(); //clear again
  203. }
  204.  
  205. private void button3_Click(object sender, EventArgs e)
  206. {
  207. con.Open();
  208. cmd = new SqlCommand(@"INSERT INTO Products
  209. (prodName, prodPrice)
  210. VALUES('" + txtprodName.Text + "','" + txtprodPrice.Text + "')", con);
  211.  
  212. cmd.ExecuteNonQuery();
  213. con.Close();
  214.  
  215. MessageBox.Show("Products Added!");
  216. loadProducts();
  217. txtprodName.Clear();
  218. txtprodPrice.Clear();
  219. txtprodID.Clear();
  220.  
  221. }
  222.  
  223. private void button4_Click(object sender, EventArgs e)
  224. {
  225. con.Open();
  226. cmd = new SqlCommand("UPDATE Products SET prodName = '" + txtprodName.Text + "', prodPrice = '" + txtprodPrice.Text + "' WHERE prodID = '" + txtprodID.Text
  227. + "';", con); // same shit above, but its update now
  228. cmd.ExecuteNonQuery();
  229. con.Close();
  230. MessageBox.Show("Update Succesful");
  231. loadProducts(); //received the new input and just refresh again for it to show.
  232. txtprodName.Clear();
  233. txtprodPrice.Clear();
  234. txtprodID.Clear(); //clear again
  235.  
  236. }
  237.  
  238.  
  239. // SEARCH
  240.  
  241.  
  242. private void SearchCustomer()
  243. {
  244. dataCustomer.Rows.Clear();
  245. con.Open();
  246. cmd = new SqlCommand(
  247. @"SELECT * FROM Customer WHERE custLname LIKE '%" + txtCSearch.Text
  248. + "%' OR custFname LIKE '%" + txtCSearch.Text
  249. + "%' OR custMI Like '%" + txtCSearch.Text
  250. + "%' OR custID LIKE '%" + txtCSearch.Text
  251. + "%';", con);
  252. rdr = cmd.ExecuteReader();
  253. while (rdr.Read())
  254. {
  255. dataCustomer.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString(), rdr[3].ToString());
  256. }
  257.  
  258. con.Close();
  259. }
  260.  
  261.  
  262. private void SearchProducts()
  263. {
  264. dataProducts.Rows.Clear();
  265. con.Open();
  266. cmd = new SqlCommand(
  267. @"SELECT * FROM Products WHERE prodName LIKE '%" + txtPSearch.Text
  268. + "%' OR prodPrice LIKE '%" + txtPSearch.Text
  269. + "%' OR prodID LIKE '%" + txtPSearch.Text
  270. + "%';", con);
  271. rdr = cmd.ExecuteReader();
  272. while (rdr.Read())
  273. {
  274. dataProducts.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString());
  275. }
  276.  
  277. con.Close();
  278. }
  279.  
  280. private void SearchPurchase()
  281. {
  282. dataPurchase.Rows.Clear();
  283. con.Open();
  284. cmd = new SqlCommand(
  285. @"SELECT * FROM Purchase WHERE custID LIKE '%" + txtTSearch.Text //TSearch instead of PSearch coz PSearch is taken by Product
  286. + "%' OR prodID LIKE '%" + txtTSearch.Text
  287. + "%' OR DatePurchased Like '%" + txtTSearch.Text
  288. + "%';", con);
  289. rdr = cmd.ExecuteReader();
  290. while (rdr.Read())
  291. {
  292. dataPurchase.Rows.Add(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString(), rdr[3].ToString());
  293. }
  294.  
  295. con.Close();
  296. }
  297.  
  298. private void btnCSearch_Click(object sender, EventArgs e)
  299. {
  300. SearchCustomer();
  301. }
  302.  
  303. private void btnPSearch_Click(object sender, EventArgs e)
  304. {
  305. SearchProducts();
  306. }
  307.  
  308. private void btnTSearch_Click(object sender, EventArgs e)
  309. {
  310. SearchPurchase();
  311. }
  312.  
  313. private void dataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e)
  314. {
  315.  
  316. }
  317. }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement