Advertisement
Guest User

Samplw

a guest
Sep 12th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 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.  
  11. namespace WindowsFormsApplication11
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19. MySql.Data.MySqlClient.MySqlConnection conn;
  20. string myConnectionString;
  21.  
  22. public void Initialize()
  23. {
  24. myConnectionString = "data source =localhost;user=root;password=;database=databaseairlines;";
  25. conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
  26. }
  27. private bool OpenConnection()
  28. {
  29. try
  30. {
  31. conn.Open();
  32. return true;
  33. }
  34. catch (MySql.Data.MySqlClient.MySqlException ex)
  35. {
  36. MessageBox.Show(ex.Message);
  37. return false;
  38. }
  39. }
  40.  
  41. private bool CloseConnection()
  42. {
  43. try
  44. {
  45. conn.Close();
  46. return true;
  47. }
  48. catch (MySqlException ex)
  49. {
  50. MessageBox.Show(ex.Message);
  51. return false;
  52. }
  53. }
  54.  
  55.  
  56. public void Populate_ListView(string myquery)
  57. {
  58. listView1.Items.Clear();
  59. ListViewItem iItem;
  60. string query = myquery;
  61. if (this.OpenConnection() == true)
  62. {
  63. try
  64. {
  65. MySqlCommand cmd = new MySqlCommand(query, conn);
  66. MySqlDataReader dataReader = cmd.ExecuteReader();
  67. while (dataReader.Read())
  68. {
  69. iItem = new ListViewItem(dataReader[0].ToString());
  70. iItem.SubItems.Add(dataReader[1].ToString());
  71. iItem.SubItems.Add(dataReader[2].ToString());
  72. iItem.SubItems.Add(dataReader[3].ToString());
  73. iItem.SubItems.Add(dataReader[4].ToString());
  74. iItem.SubItems.Add(dataReader[6].ToString());
  75. iItem.SubItems.Add(dataReader[7].ToString());
  76. iItem.SubItems.Add(dataReader[8].ToString());
  77. iItem.SubItems.Add(dataReader[9].ToString());
  78. iItem.SubItems.Add(dataReader[10].ToString());
  79. iItem.SubItems.Add(dataReader[11].ToString());
  80. iItem.SubItems.Add(dataReader[12].ToString());
  81. iItem.SubItems.Add(dataReader[13].ToString());
  82. iItem.SubItems.Add(dataReader[14].ToString());
  83. iItem.SubItems.Add(dataReader[15].ToString());
  84. iItem.SubItems.Add(dataReader[16].ToString());
  85. listView1.Items.Add(iItem);
  86.  
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. MessageBox.Show(ex.Message);
  92. }
  93. finally
  94. {
  95. this.CloseConnection();
  96. }
  97. listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
  98. listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
  99. }
  100. }
  101.  
  102.  
  103.  
  104.  
  105. public void Insert()
  106. {
  107. string query = "INSERT INTO info(ID,Name,Contact_Number,Address,Time_Flight,Time_Flight,MM,DD,YY,Book destination,To,From,Destination_Cost,Promo,Adult,Children,Infant,Total_Cost) VALUES ('" + comboBox1.Text + "', '" + nameTextBox.Text + "', '" + contact_NumberTextBox.Text + "', '" + addressTextBox.Text + "', '" + comboBox1.Text + "', '" + time_FlightComboBox.Text + "', '" + date_Arrival__MM_TextBox.Text + "', '" + dDTextBox.Text + "', '" + yYTextBox.Text + "', '" + destination_ComboBox.Text + "', '" + toComboBox.Text + "', '" + fromComboBox.Text + "', '" + destination_CostTextBox.Text + "', '" + promoComboBox.Text + "', '" + adultTextBox.Text + "', '" + childrenTextBox.Text + "', '" + infantTextBox.Text + "')";
  108. if (this.OpenConnection() == true)
  109. {
  110. try
  111. {
  112. MySqlCommand cmd = new MySqlCommand(query, conn);
  113. cmd.ExecuteNonQuery();
  114. MessageBox.Show("Account Has Been Created!", "Insert Successful");
  115. }
  116. catch (MySqlException ex)
  117. {
  118. MessageBox.Show(ex.Message);
  119. }
  120. finally
  121. {
  122. this.CloseConnection();
  123. Clear();
  124.  
  125. }
  126. }
  127. }
  128. private void Clear()
  129. {
  130. contact_NumberTextBox.Text = "";
  131. time_FlightComboBox.Text = "";
  132. date_Arrival__MM_TextBox.Text = "";
  133. nameTextBox.Text = "";
  134. addressTextBox.Text = "";
  135. time_FlightComboBox.Text = "";
  136. date_Arrival__MM_TextBox.Text = "";
  137. dDTextBox.Text = "";
  138. yYTextBox.Text = "";
  139. destination_ComboBox.SelectedIndex = -1;
  140. toComboBox.Text = "";
  141. fromComboBox.Text = "";
  142. destination_CostTextBox.Text = "";
  143. promoComboBox.Text = "";
  144. adultTextBox.Text = "";
  145. childrenTextBox.Text = "";
  146. infantTextBox.Text = "";
  147.  
  148. comboBox1.SelectedIndex = -1;
  149. comboBox2.SelectedIndex = -1;
  150.  
  151. }
  152. private void button2_Click(object sender, EventArgs e)
  153. {
  154. if (MessageBox.Show("Do you really want to Insert Data?", "Data Inserted", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  155. {
  156. Insert();
  157. Populate_ListView("SELECT * FROM databaseforairlies");
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement