Public feed #892489

Untitled

public text v1 · immutable
Raw Edit
·published 2008-02-05 14:02 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
//using MySql.Data.MySqlClient;

namespace Cars
{
    public partial class Form1 : Form
    {
        public string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NEWCarRentals.mdb";
        //string MyConString = "SERVER=localhost;" +
        //        "DATABASE=CarRental;" +
        //        "UID=root;" +
        //        "PASSWORD=;";
        public ArrayList AllCars;
        public CarCharacteristics tmp;
        public int Available = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            LoadDatabase();
        }

        private void LoadDatabase()
        {
            string strSelect = @"SELECT Brands.Brand, Models.Model, Models.Category, Models.Availability,
                                 Models.Rented
                                    FROM Brands
                                        INNER JOIN Models
                                            ON Brands.BrandID = Models.BrandID";

            try
            {
                DataSet myDataSet = new DataSet();
                OleDbConnection myAccessConn = null;
                myAccessConn = new OleDbConnection(strAccessConn);
                OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strSelect, myAccessConn);
                myDataAdapter.Fill(myDataSet, "Brands");
                //MySqlConnection mySqlConn = new MySqlConnection(MyConString);
                //MySqlDataAdapter mySqlAdapter = new MySqlDataAdapter(strSelect, mySqlConn);
                //mySqlAdapter.Fill(myDataSet, "Brands");
                
                AllCars = new ArrayList();

                DataRowCollection dra = myDataSet.Tables["Brands"].Rows;
                foreach (DataRow dr in dra)
                {
                    //MessageBox.Show(dr[1].ToString());
                    //MessageBox.Show(dr[1].ToString());
                    tmp = new CarCharacteristics();
                    tmp.Brand = dr[0].ToString();
                    tmp.Model = dr[1].ToString();
                    tmp.Category = dr[2].ToString();
                    tmp.Availability = System.Convert.ToInt32(dr[3]);
                    tmp.Rented = System.Convert.ToInt32(dr[4]);
                    //tmp.Name = dr[5].ToString();
                    //tmp.Lastname = dr[6].ToString();
                    //tmp.phone = dr[7].ToString();
                    AllCars.Add(tmp);
                }
                for (int i = 0; i < AllCars.Count; i++)
                {
                    tmp = (CarCharacteristics)AllCars[i];
                    this.BrandName.Items.Add(tmp.Brand);
                    //if (BrandName.Items[i].ToString() == tmp.Brand)
                    //{
                    //    MessageBox.Show(BrandName.Items[i].ToString());
                    //}
                }
                //for (int i = 0; i < BrandName.Items.Count; i++)
                //{
                //    MessageBox.Show(BrandName.Items[i].ToString());
                //}
                //for (int i = 0; i < BrandName.Items.Count; i++)
                //{
                //    //MessageBox.Show(BrandName.Items[i].ToString());
                //    if (BrandName.Items[i].ToString() == BrandName.Items[i + 1].ToString())
                //        BrandName.Items.Remove(BrandName.Items[i].ToString());
                //}
                BrandName.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString());
            }
        }

        private void BrandName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModelsName.Items.Clear();
            ModelsName.Sorted = true;
            CategoryRent.Items.Clear();

            foreach (CarCharacteristics a in AllCars)
            {
                if (BrandName.SelectedItem.ToString() == a.Brand)
                {
                      ModelsName.Items.Add(a.Model);
                }
            }
            ModelsName.SelectedIndex = 0;
        }

        private void RentButton_Click(object sender, EventArgs e)
        {
            if (BrandName.SelectedIndex == -1)
                BrandName.Focus();
            else if (ModelsName.SelectedIndex == -1)
                ModelsName.Focus();
            else if (CategoryRent.SelectedIndex == -1)
                CategoryRent.Focus();
            else if (NameTextBox.Text.Length == 0)
                NameTextBox.Focus();
            else if (LastnameTextBox.Text.Length == 0)
                LastnameTextBox.Focus();
            else if(PhoneTextBox.Text.Length == 0)
                PhoneTextBox.Focus();
            else
            {
                foreach (CarCharacteristics a in AllCars)
                {
                    if (BrandName.SelectedItem.ToString() == a.Brand &&
                        ModelsName.SelectedItem.ToString() == a.Model &&
                        CategoryRent.SelectedItem.ToString() == a.Category)
                    {
                        if (a.Rented < a.Availability)
                        {
                            a.Name = NameTextBox.Text;
                            a.Lastname = LastnameTextBox.Text;
                            a.phone = PhoneTextBox.Text;
                            a.Rented++;
                            Available = a.Availability - a.Rented;
                            AvailableTextBox.Text = Available.ToString();
                            //MessageBox.Show("O " + a.Name + " " + a.Lastname + " me thlefono " +
                            //    a.phone + " Nikiase to " + a.Brand + " " + a.Model + " " + a.Category);
                        }
                        else
                            MessageBox.Show("Not available");
                    }
                }
            }
        }

        private void UpdateButton_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection myConn = new OleDbConnection(strAccessConn);
                OleDbCommand UPDATECommand = new OleDbCommand();
                OleDbCommand INSERTCommand = new OleDbCommand();
                UPDATECommand.Connection = myConn;
                INSERTCommand.Connection = myConn;
                myConn.Open();

                foreach (CarCharacteristics a in AllCars)
                {
                    UPDATECommand.CommandText = @"UPDATE Models 
                                              SET Models.Rented=" + a.Rented +
                                                " WHERE Models.BrandID=(SELECT BrandID From Brands WHERE Brand='" + a.Brand + "')"
                                                  + " AND Models.Model='" + a.Model + "'"
                                                   + " AND Category='" + a.Category + "'";


                    INSERTCommand.CommandText = @"INSERT INTO Customers 
                                                     ( cName, cLastname, cPhone, ModelID )
                           SELECT '" + a.Name + "', '" + a.Lastname + "', '" + a.phone + "'," 
                              +  " ModelID FROM Models WHERE Model ='" + a.Model + "'";

                    //MessageBox.Show(UPDATECommand.CommandText);
                    UPDATECommand.ExecuteNonQuery();
                    INSERTCommand.ExecuteNonQuery();
                }
                myConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString());
            }

           
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            bool BrandFlag = false;
            bool Cancel = false;
            bool Show = false;
            DialogResult result;

            if (BrandTextBox.Text.Length == 0)
                BrandTextBox.Focus();
            else if (ModelTextBox.Text.Length == 0)
                ModelTextBox.Focus();
            else if (CategoryAdd.SelectedIndex == -1)
                CategoryAdd.Focus();
            else if (CountTextBox.Text.Length == 0)
                CountTextBox.Focus();
            else
            {
                try
                {
                    foreach (CarCharacteristics a in AllCars)
                    {
                        
                        if (a.Brand == BrandTextBox.Text || a.Model == ModelTextBox.Text)
                        {
                            Show = true;
                            if (a.Brand == BrandTextBox.Text && a.Model == ModelTextBox.Text)
                            {
                                MessageBox.Show(BrandTextBox.Text + " " + ModelTextBox.Text + " already exist");
                                Cancel = true;
                                Show = false;
                            }
                            if (Show)
                            {
                                result = MessageBox.Show("Brand or Model already exist, Are you sure?", "Confirm", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                    BrandFlag = true;
                                else
                                    Cancel = true;
                            }
                        }
                        //else
                        //    MessageBox.Show("NOT in DATABASE");
                    }
                    if (BrandFlag == true && !Cancel)
                    {
                        try
                        {
                            OleDbConnection myConn = new OleDbConnection(strAccessConn);
                            OleDbCommand command = new OleDbCommand();
                            command.Connection = myConn;
                            command.CommandText = @"INSERT INTO Models 
                            ( Model, Category, Availability, Rented, BrandID)
                                SELECT '" + ModelTextBox.Text + "','" + CategoryAdd.SelectedItem.ToString() + "'," + Convert.ToInt32(CountTextBox.Text) + ",0,"
                                       + "BrandID FROM Brands WHERE Brand='" + BrandTextBox.Text + "'";
                            myConn.Open();
                            command.ExecuteNonQuery();
                            myConn.Close();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Error: " + ex.Message.ToString());
                        }
                    }
                    if(!BrandFlag && !Cancel)
                    {
                        OleDbConnection myConn = new OleDbConnection(strAccessConn);
                        OleDbCommand InsertBrandCommand = new OleDbCommand();
                        OleDbCommand InsertModelCommand = new OleDbCommand();
                        InsertBrandCommand.Connection = myConn;
                        InsertModelCommand.Connection = myConn;

                        InsertBrandCommand.CommandText = @"INSERT INTO Brands (Brand) VALUES
                                                ('" + BrandTextBox.Text + "');"; 


                        InsertModelCommand.CommandText = @"INSERT INTO Models 
                                ( Model, Category, Availability, Rented, BrandID )
                                  SELECT '" + ModelTextBox.Text + "','" + CategoryAdd.SelectedItem.ToString() + " '," + Convert.ToInt32(CountTextBox.Text) + ",0,"
                       + "BrandID From Brands WHERE Brand='" + BrandTextBox.Text + "'";

                        myConn.Open();
                        InsertBrandCommand.ExecuteNonQuery();
                        InsertModelCommand.ExecuteNonQuery();
                        myConn.Close();
                    }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message.ToString());
                }
            }
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            BrandName.Items.Clear();
            BrandName.Text = "";
            ModelsName.Items.Clear();
            ModelsName.Text = "";
            CategoryRent.Items.Clear();
            CategoryRent.Text = "";
            AvailableTextBox.Clear();
            NameTextBox.Clear();
            LastnameTextBox.Clear();
            PhoneTextBox.Clear();
            LoadDatabase();
        }

        private void ModelsName_SelectedIndexChanged(object sender, EventArgs e)
        {
            CategoryRent.Sorted = true;
            CategoryRent.Items.Clear();

            foreach (CarCharacteristics a in AllCars)
            {
                if (BrandName.SelectedItem.ToString() == a.Brand &&
                    ModelsName.SelectedItem.ToString() == a.Model)
                {
                    CategoryRent.Items.Add(a.Category);
                    CategoryRent.SelectedItem=a.Category;
                }
            }
        }

        private void CategoryRent_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (CarCharacteristics a in AllCars)
            {
                if (BrandName.SelectedItem.ToString() == a.Brand &&
                    ModelsName.SelectedItem.ToString() == a.Model &&
                    CategoryRent.SelectedItem.ToString() == a.Category)
                {
                    Available = a.Availability - a.Rented;
                    AvailableTextBox.Text = Available.ToString();
                }
            }
        }
    }
}