Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- namespace WindowsFormsApplication11
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- MySql.Data.MySqlClient.MySqlConnection conn;
- string myConnectionString;
- public void Initialize()
- {
- myConnectionString = "data source =localhost;user=root;password=;database=databaseairlines;";
- conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
- }
- private bool OpenConnection()
- {
- try
- {
- conn.Open();
- return true;
- }
- catch (MySql.Data.MySqlClient.MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- conn.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- public void Populate_ListView(string myquery)
- {
- listView1.Items.Clear();
- ListViewItem iItem;
- string query = myquery;
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- MySqlDataReader dataReader = cmd.ExecuteReader();
- while (dataReader.Read())
- {
- iItem = new ListViewItem(dataReader[0].ToString());
- iItem.SubItems.Add(dataReader[1].ToString());
- iItem.SubItems.Add(dataReader[2].ToString());
- iItem.SubItems.Add(dataReader[3].ToString());
- iItem.SubItems.Add(dataReader[4].ToString());
- iItem.SubItems.Add(dataReader[6].ToString());
- iItem.SubItems.Add(dataReader[7].ToString());
- iItem.SubItems.Add(dataReader[8].ToString());
- iItem.SubItems.Add(dataReader[9].ToString());
- iItem.SubItems.Add(dataReader[10].ToString());
- iItem.SubItems.Add(dataReader[11].ToString());
- iItem.SubItems.Add(dataReader[12].ToString());
- iItem.SubItems.Add(dataReader[13].ToString());
- iItem.SubItems.Add(dataReader[14].ToString());
- iItem.SubItems.Add(dataReader[15].ToString());
- iItem.SubItems.Add(dataReader[16].ToString());
- listView1.Items.Add(iItem);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- this.CloseConnection();
- }
- listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
- listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
- }
- }
- public void Insert()
- {
- 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 + "')";
- if (this.OpenConnection() == true)
- {
- try
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Account Has Been Created!", "Insert Successful");
- }
- catch (MySqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- this.CloseConnection();
- Clear();
- }
- }
- }
- private void Clear()
- {
- contact_NumberTextBox.Text = "";
- time_FlightComboBox.Text = "";
- date_Arrival__MM_TextBox.Text = "";
- nameTextBox.Text = "";
- addressTextBox.Text = "";
- time_FlightComboBox.Text = "";
- date_Arrival__MM_TextBox.Text = "";
- dDTextBox.Text = "";
- yYTextBox.Text = "";
- destination_ComboBox.SelectedIndex = -1;
- toComboBox.Text = "";
- fromComboBox.Text = "";
- destination_CostTextBox.Text = "";
- promoComboBox.Text = "";
- adultTextBox.Text = "";
- childrenTextBox.Text = "";
- infantTextBox.Text = "";
- comboBox1.SelectedIndex = -1;
- comboBox2.SelectedIndex = -1;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("Do you really want to Insert Data?", "Data Inserted", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- Insert();
- Populate_ListView("SELECT * FROM databaseforairlies");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement