예제 #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            string      selectedBikes    = null;
            List <Bike> selectedBikeList = new List <Bike>();
            decimal     totalPrice       = 0;

            foreach (TreeNode node in this.treeView1.Nodes)
            {
                foreach (TreeNode sub in node.Nodes)
                {
                    if (sub.Checked)
                    {
                        int  id = Convert.ToInt32(sub.Text);
                        Bike b  = AllBikeList[id - 1001];
                        totalPrice    += b.HourlyPrice;
                        selectedBikes += b.BID + ", ";
                        selectedBikeList.Add(b);
                    }
                }
            }

            if (selectedBikes == null || string.IsNullOrWhiteSpace(textBox8.Text) || containsLetter(textBox8.Text))
            {
                MessageBox.Show("Please make sure that you enter a numerical value in the expecteed time field!");
            }
            else
            {
                selectedBikes = selectedBikes.Remove(selectedBikes.Length - 2);
                DialogResult result = MessageBox.Show(string.Format("Are you sure you would like to rent bikes " + selectedBikes + " at an estimated price of ${0:#.00}?", Convert.ToDecimal(totalPrice) * Convert.ToDecimal(textBox8.Text)), "ATTENTION!", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    Customer c = CustomerList[this.listBox3.SelectedIndex];

                    ConnectionInfo = String.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;
                    AttachDbFilename=|DataDirectory|\{0};Integrated Security=True;",
                                                   filename);

                    db = new SqlConnection(ConnectionInfo);
                    db.Open();

                    foreach (Bike b in selectedBikeList)
                    {
                        string sql = string.Format(@"INSERT INTO 
Rentals(CID,BID,StartTime,ExpDuration)
Values((SELECT CID from Customers WHERE CID = '{0}'),
(SELECT BID from Bikes WHERE BID = '{1}'),
'{2}',
'{3}');

UPDATE Bikes
Set Rented = 1
Where BID = '{1}';
                    ", c.CID, b.BID, DateTime.Now, Convert.ToDecimal(textBox8.Text));;

                        //MessageBox.Show(sql);

                        data.ExecuteActionQuery(sql);
                    }


                    db.Close();

                    MessageBox.Show("success");

                    treeView1.Nodes.Clear();
                    treeView1.Refresh();

                    textBox8.Clear();
                    textBox8.Refresh();
                }
                else
                {
                }
            }
        }
예제 #2
0
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox18.Clear();
            textBox19.Clear();
            textBox20.Clear();
            textBox21.Clear();

            string test = listBox2.GetItemText(listBox2.SelectedItem);
            int    i    = Convert.ToInt32(test.Substring(0, 4));
            Bike   b    = AllBikeList[i - 1001];

            this.textBox20.Text = Convert.ToString(b.Year);
            this.textBox19.Text = Convert.ToString(b.Description);
            this.textBox18.Text = string.Format("${0:#.00}", Convert.ToDecimal(b.HourlyPrice));

            if (b.Rented == true)
            {
                radioButton4.Checked = true;

                ConnectionInfo = String.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;
            AttachDbFilename=|DataDirectory|\{0};Integrated Security=True;",
                                               filename);

                db = new SqlConnection(ConnectionInfo);
                db.Open();

                string sql = string.Format(@"select Rentals.BID, Rentals.ExpDuration, Rentals.StartTime
from Rentals
Inner Join Bikes on Bikes.BID = Rentals.BID
Inner Join BikeTypes on Bikes.TID = BikeTypes.TID
Where Rentals.BID = '{0}';", b.BID);

                //MessageBox.Show(sql);

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = db;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet        ds      = new DataSet();

                cmd.CommandText = sql;
                adapter.Fill(ds);

                db.Close();

                int    bid;
                double duration  = 0.0;
                string startTime = null;
                foreach (DataRow row in ds.Tables["TABLE"].Rows)
                {
                    this.listBox5.Items.Add(row["BID"]);

                    bid       = Convert.ToInt32(row["BID"]);
                    duration  = Convert.ToDouble(row["ExpDuration"]);
                    startTime = Convert.ToString(row["StartTime"]);
                }

                TimeSpan dur      = TimeSpan.FromHours(duration);
                DateTime start    = DateTime.Parse(startTime);
                DateTime timeLeft = start.Add(dur);
                this.textBox21.Text = Convert.ToString(timeLeft);
            }

            else
            {
                radioButton3.Checked = true;
            }
        }