Advertisement
raaj5671

ViewModel code

Sep 19th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using shopfloorcs.Enum;
  3. using shopfloorcs.Models;
  4. using shopfloorcs.ViewModels.Commands;
  5. using shopfloorcs.Views;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15.  
  16. namespace shopfloorcs.ViewModels
  17. {
  18. public class ProductionLineConfigViewModel : INotifyPropertyChanged
  19. {
  20. Database db = new Database();
  21. MySqlDataReader reader;
  22. MySqlDataAdapter da;
  23. DataTable dt = new DataTable();
  24.  
  25. private ProductionLineConfig productionlineconfig;
  26.  
  27. public ProductionLineConfig ProductionLineConfig
  28. {
  29. get { return productionlineconfig; }
  30.  
  31. set
  32. {
  33. productionlineconfig = value;
  34. OnPropertyChanged("ProductionLineConfig");
  35. }
  36. }
  37.  
  38. // TODO - List all productionline configs; Implement observablecollections
  39. public List<ProductionLineConfig> listAllProductionLineConfigs
  40. {
  41. get
  42. {
  43. var plc = new List<ProductionLineConfig>();
  44. string query;
  45. query = "select * from productionlineconfig";
  46. da = new MySqlDataAdapter(query, db.GetConnection());
  47. da.Fill(dt);
  48. reader = db.QueryCommand(query);
  49. while (reader.Read())
  50. {
  51. plc.Add(new ProductionLineConfig()
  52. {
  53. ProductionLineId = Int32.Parse(reader[0].ToString()),
  54. ProductionLineCode = reader[1].ToString(),
  55. ProductionLineName = reader[2].ToString(),
  56. ProductionLineStatus = Convert.ToBoolean(reader[3].ToString()),
  57. ProductionLineCreatedDate = Convert.ToDateTime(reader[4].ToString())
  58.  
  59. });
  60. }
  61.  
  62.  
  63. reader.Close();
  64.  
  65. return plc;
  66. }
  67. }
  68.  
  69. // TODO - Create new productionline config;
  70. public void createNewProductionLineConfig()
  71. {
  72. string query;
  73.  
  74. try
  75. {
  76. query = "Insert into productionlineconfig (PRODUCTION_LINE_CODE, PRODUCTION_LINE_NAME, PRODUCTION_LINE_STATUS) Values ('" + ProductionLineConfig.ProductionLineCode + "' , '" + ProductionLineConfig.ProductionLineName + "' , '" + Convert.ToInt32(ProductionLineConfig.ProductionLineStatus) + "')";
  77.  
  78. db.QueryCommand(query);
  79.  
  80. MessageBox.Show("User created successfully");
  81. production_line_config plcWindow = new production_line_config();
  82. plcWindow.Hide();
  83. npi_home npiWindow = new npi_home();
  84. npiWindow.Show();
  85.  
  86. }
  87. catch (MySqlException ex)
  88. {
  89. Console.WriteLine("MySQL ERROR: " + ex.ToString());
  90. }
  91. }
  92.  
  93. // TODO - Update productionline config;
  94. public void updateProductionLineConfig()
  95. {
  96. string query;
  97.  
  98. try
  99. {
  100. query = "Update productionlineconfig set PRODUCTION_LINE_NAME = '" + ProductionLineConfig.ProductionLineName + "', PRODUCTION_LINE_STATUS = '" + Convert.ToInt32(ProductionLineConfig.ProductionLineStatus) + "' where ID = " + ProductionLineConfig.ProductionLineId;
  101. MessageBox.Show("Name:" + ProductionLineConfig.ProductionLineName + "\nStatus: " + Convert.ToInt32(ProductionLineConfig.ProductionLineStatus) + "\nID: " + ProductionLineConfig.ProductionLineId);
  102. // update not working....
  103. db.QueryCommand(query);
  104.  
  105. MessageBox.Show("Record updated successfully");
  106. production_line_config plcWindow = new production_line_config();
  107. plcWindow.Hide();
  108. npi_home npiWindow = new npi_home();
  109. npiWindow.Show();
  110.  
  111. }
  112. catch (MySqlException ex)
  113. {
  114. Console.WriteLine("MySQL Error: " + ex.ToString());
  115. }
  116. }
  117.  
  118.  
  119. public Status status = Status.Enable;
  120.  
  121. public List<string> Statusstring {
  122. get
  123. {
  124. return System.Enum.GetNames(typeof(Status)).ToList();
  125.  
  126. }
  127.  
  128. }
  129.  
  130.  
  131. public NewProductionLineConfigCommand newProductionLineConfigCommand { get; set; }
  132. public UpdateProductionLineConfigCommand updateProductionLineConfigCommand { get; set; }
  133.  
  134. public ProductionLineConfigViewModel()
  135. {
  136. ProductionLineConfig = new ProductionLineConfig();
  137. newProductionLineConfigCommand = new NewProductionLineConfigCommand(this);
  138. updateProductionLineConfigCommand = new UpdateProductionLineConfigCommand(this);
  139. }
  140.  
  141. public event PropertyChangedEventHandler PropertyChanged;
  142.  
  143. private void OnPropertyChanged(string propertyName)
  144. {
  145. if(PropertyChanged != null)
  146. {
  147. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  148. }
  149. }
  150.  
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement