示例#1
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            cxt = new SportsFacilitiesEntities();
            fac = new Facility();
            SubFacility subf = new SubFacility();
            Rate        rate = new Rate();
            string      r;

            try
            {
                fac = cxt.Facilities.Where(x => x.facilityname == cmbSearchBy.Text).First();
                gbSearchResults.Visible = true;
                //Facility Name / ID Display
                txtFacilityID.Text = fac.facilityid.ToString();
                facilityidtemp     = txtFacilityID.Text;
                txtName.Text       = fac.facilityname.ToString();
                nametemp           = txtName.Text;
                // Rates Display
                r             = fac.rateid.ToString();
                rate          = cxt.Rates.Where(x => x.rateid.ToString() == r).First();
                ratesWOsymbol = rate.ratepertimeslot.ToString();
                cmbRates.Text = "$ " + ratesWOsymbol + ".00";
                ratestemp     = ratesWOsymbol;
                // Subfacility Display
                var q = from x in cxt.SubFacilities where x.facilityid.ToString() == txtFacilityID.Text select x;
                txtFacilitiesNo.Text = q.Count().ToString();
                facilitiesnotemp     = txtFacilitiesNo.Text;
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Search Results cannot be empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void InsertData()
        {
            ctx      = new SportsFacilitiesEntities();
            facility = new Facility();
            facility.facilityname = txtName.Text;
            string price = "";

            if (cmbRates.Text.Length > 6)
            {
                price = cmbRates.Text.Substring(2, 2);
            }
            else
            {
                price = cmbRates.Text.Substring(2, 1);
            }
            int rate = Convert.ToInt32(price);

            if ((ctx.Rates.Where(x => x.ratepertimeslot == rate).Count()) > 0)
            {
                facility.rateid = ctx.Rates.Where(x => x.ratepertimeslot == rate).First().rateid;
            }
            else
            {
                Rate newrate = new Rate();
                newrate.ratepertimeslot = rate;
                ctx.Rates.Add(newrate);
                ctx.SaveChanges();
                facility.rateid = ctx.Rates.Where(x => x.ratepertimeslot == rate).First().rateid;
            }
            try
            {
                ctx.Facilities.Add(facility);
                int noOfCourt = Convert.ToInt32(cmbCourtNo.SelectedItem.ToString());
                for (int i = 0; i < noOfCourt; i++)
                {
                    SubFacility sf = new SubFacility();
                    sf.facilityid = facility.facilityid;
                    if (i == 0)
                    {
                        sf.subfacilityname = "A";
                    }
                    else if (i == 1)
                    {
                        sf.subfacilityname = "B";
                    }
                    else
                    {
                        sf.subfacilityname = "C";
                    }
                    ctx.SubFacilities.Add(sf);
                }
                ctx.SaveChanges();
            }
            catch
            {
                MessageBox.Show("Error Occurred. Please Try Again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }