Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10. using MySql.Data.Types;
  11.  
  12. namespace QC_Paper_Defect
  13. {
  14. public partial class MenuUser : Form
  15. {
  16. MySqlConnection connection;
  17. MySqlCommand myComm = new MySqlCommand();
  18.  
  19. public MenuUser()
  20. {
  21. InitializeComponent();
  22. connection = new MySqlConnection("server=localhost;user id=root;password=;database=ta;");
  23.  
  24. //Get all items in datatable
  25. DataTable DTItems = GetAllItems();
  26.  
  27. //Fill grid with items
  28. dataGridView1.DataSource = DTItems;
  29.  
  30.  
  31.  
  32. }
  33. DataTable GetAllItems()
  34. {
  35. try
  36. {
  37. //prepare query to get all records from items table
  38. string query = "select * from tb_user";
  39. //prepare adapter to run query
  40.  
  41. MySqlDataAdapter adapter = new MySqlDataAdapter(query, connection);
  42. DataSet DS = new DataSet();
  43. //get query results in dataset
  44. adapter.Fill(DS);
  45. //return datatable with all records
  46. return DS.Tables[0];
  47.  
  48. }
  49. catch (Exception ex)
  50. {
  51. //MessageBox.Show(ex.Message);
  52. Console.WriteLine(ex.Message);
  53. }
  54. return null;
  55. }
  56.  
  57. private void btn_tmbh_Click(object sender, EventArgs e)
  58. {
  59. connection.Open();
  60. myComm.Connection = connection;
  61. string SQL = "INSERT INTO tb_user (user_id,name,password,alamat,no_tlp,jabatan,level) VALUES ('"+tf_user.Text+"','"+tf_name.Text+"','"+tf_pass.Text+"','"+tf_alamat.Text+"','"+tf_tlp.Text+"','"+tf_jabatan.Text+"','"+tf_level.Text+"');";
  62. myComm.CommandText = SQL;
  63.  
  64. try
  65. {
  66. myComm.ExecuteNonQuery();
  67. dataGridView1.DataSource = GetAllItems();
  68. // scrip Auto scroll ini trh mana???
  69. dataGridView1.FirstDisplayedCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
  70. //MessageBox.Show("Added", "MySQL says");
  71. Console.WriteLine("Added: " + tf_user.Text + "," + tf_name.Text + "," + tf_pass.Text + "," + tf_alamat.Text + "," + tf_tlp.Text + "," + tf_jabatan.Text + "," + tf_level );
  72. }
  73. catch (MySqlException ex)
  74. {
  75. //MessageBox.Show("Unable to add because: " + ex.Message , "MySQL says");
  76. Console.WriteLine("Unable to add because: " + ex.Message);
  77. }
  78. connection.Close();
  79.  
  80. }
  81.  
  82. private void Simpan_Click(object sender, EventArgs e)
  83. {
  84. connection.Open();
  85. myComm.Connection = connection;
  86. string SQL = "INSERT INTO tb_user (user_id,name,password,alamat,no_tlp,jabatan,level) VALUES ('" + tf_user.Text + "','" + tf_name.Text + "','" + tf_pass.Text + "','" + tf_alamat.Text + "','" + tf_tlp.Text + "','" + tf_jabatan.Text + "','" + tf_level.Text + "');";
  87. myComm.CommandText = SQL;
  88.  
  89. try
  90. {
  91. myComm.ExecuteNonQuery();
  92. dataGridView1.DataSource = GetAllItems();
  93. //MessageBox.Show("Added", "MySQL says");
  94. Console.WriteLine("Added: " + tf_user.Text + "," + tf_name.Text + "," + tf_pass.Text + "," + tf_alamat.Text + "," + tf_tlp.Text + "," + tf_jabatan.Text + "," + tf_level);
  95. }
  96. catch (MySqlException ex)
  97. {
  98. //MessageBox.Show("Unable to add because: " + ex.Message , "MySQL says");
  99. Console.WriteLine("Unable to add because: " + ex.Message);
  100. }
  101. connection.Close();
  102. }
  103.  
  104. private void btn_batal_Click(object sender, EventArgs e)
  105. {
  106. Close();
  107. }
  108.  
  109. private void btn_hps_Click(object sender, EventArgs e)
  110. {
  111. connection.Open();
  112. myComm.Connection = connection;
  113. string SQL = "DELETE from tb_user WHERE id_user='"+ tf_user.Text +"';";
  114. connection.Close();
  115. }
  116.  
  117. private void btn_ubah_Click(object sender, EventArgs e)
  118. {
  119. connection.Open();
  120. myComm.Connection = connection;
  121. string SQL = "UPDATE tb_user SET name = '" + tf_name.Text + "' WHERE id_user = '" + tf_user.Text + "';";
  122. connection.Close();
  123.  
  124. string selectedItem = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
  125. tf_user.Text = selectedItem;
  126. }
  127.  
  128. private void dataGridView1_Sorted(object sender, EventArgs e)
  129. {
  130. this.dataGridView1.FirstDisplayedCell = this.dataGridView1.CurrentCell;
  131. }
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement