/*Logic for searching * on the basis of Model*/ private void BtnSearch_Click(object sender, RoutedEventArgs e) { try { if (!(Regex.IsMatch(txtmodel.Text, @"^\w+\s*\w*$"))) { MessageBox.Show("Please enter proper car model."); } else { string modName = txtmodel.Text; using (CIMSEntities2 entityobj = new CIMSEntities2()) { CAR carObj = entityobj.CARs.FirstOrDefault(i => i.Model == modName); txtEngine.Text = carObj.Engine; txtPrice.Text = carObj.Price.ToString(); txtBHP.Text = carObj.BHP.ToString(); cmbManufacturer.SelectedValue = carObj.ManufacturerId; txtAirbagDetails.Text = carObj.AirBagDetails; txtSeat.Text = carObj.Seat.ToString(); txtMileage.Text = carObj.Mileage.ToString(); txtBootspace.Text = carObj.BootSpace.ToString(); cmbType.SelectedValue = carObj.TypeId; cmbTransmission.SelectedValue = carObj.TransmissionId; } } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
/*Logic for updating * the CAR table*/ private void BtnUpdate_Click(object sender, RoutedEventArgs e) { try { using (CIMSEntities2 entityobj = new CIMSEntities2()) { string model = txtmodel.Text; CAR carObj = entityobj.CARs.FirstOrDefault(i => i.Model == model); carObj.Engine = txtEngine.Text; carObj.BootSpace = txtBootspace.Text; carObj.AirBagDetails = txtAirbagDetails.Text; carObj.BHP = Convert.ToInt32(txtBHP.Text); carObj.Mileage = Convert.ToInt32(txtMileage.Text); carObj.Seat = Convert.ToInt32(txtSeat.Text); carObj.Price = Convert.ToDecimal(txtPrice.Text); carObj.ManufacturerId = Convert.ToInt32(cmbManufacturer.SelectedValue.ToString()); carObj.TypeId = Convert.ToInt32(cmbType.SelectedValue.ToString()); carObj.TransmissionId = Convert.ToInt32(cmbTransmission.SelectedValue.ToString()); entityobj.SaveChanges(); MessageBox.Show("1 Record updated"); } } catch (Exception) { MessageBox.Show("All details should be filled before updating."); } }
/*Logic for adding new information * to the CAR table*/ private void BtnAdd_Click(object sender, RoutedEventArgs e) { try { StringBuilder sb = new StringBuilder(); int invalid = 0; using (cims) { CAR car = new CAR(); if (!(Regex.IsMatch(txtmodel.Text, @"^\w+\s*\w*$"))) { invalid = 1; sb.Append(Environment.NewLine + "Model cannot be blank and must be in proper format."); } else { car.Model = txtmodel.Text; } if (cmbManufacturer.SelectedValue != null) { car.ManufacturerId = Convert.ToInt32(cmbManufacturer.SelectedValue.ToString()); } else { invalid = 1; sb.Append(Environment.NewLine + "Please Select Manufacturer."); } if (cmbType.SelectedValue != null) { car.TypeId = Convert.ToInt32(cmbType.SelectedValue.ToString()); } else { invalid = 1; sb.Append(Environment.NewLine + "Please Select Car Type."); } if (!(Regex.IsMatch(txtEngine.Text, @"^[0-9].[0-9]L$"))) { invalid = 1; sb.Append(Environment.NewLine + "Engine should be in proper format(e.g. 3.5L)."); } else { car.Engine = txtEngine.Text; } if (!(Regex.IsMatch(txtBHP.Text, @"^\d+$"))) { invalid = 1; sb.Append(Environment.NewLine + "BHP cannot be blank and must be numeric."); } else { car.BHP = Convert.ToInt32(txtBHP.Text); } if (cmbTransmission.SelectedValue != null) { car.TransmissionId = Convert.ToInt32(cmbTransmission.SelectedValue.ToString()); } else { invalid = 1; sb.Append(Environment.NewLine + "Please Select Car Transmission."); } if (!(Regex.IsMatch(txtMileage.Text, @"^\d+$"))) { invalid = 1; sb.Append(Environment.NewLine + "Mileage cannot be blank and must be numeric."); } else { car.Mileage = Convert.ToInt32(txtMileage.Text); } if (!(Regex.IsMatch(txtSeat.Text, @"^\d+$"))) { invalid = 1; sb.Append(Environment.NewLine + "Seat cannot be blank and must be numeric."); } else { car.Seat = Convert.ToInt32(txtSeat.Text); } if (!(Regex.IsMatch(txtAirbagDetails.Text, @"^\w+$"))) { invalid = 1; sb.Append(Environment.NewLine + "Airbag details cannot be blank."); } else { car.AirBagDetails = txtAirbagDetails.Text; } if (!(Regex.IsMatch(txtBootspace.Text, @"^\d+\s*\w*$"))) { invalid = 1; sb.Append(Environment.NewLine + "Bootspace cannot be blank."); } else { car.BootSpace = txtBootspace.Text; } if (!(Regex.IsMatch(txtPrice.Text, @"^\d+$"))) { invalid = 1; sb.Append(Environment.NewLine + "Price cannot be blank and must be numeric."); } else { car.Price = Convert.ToInt64(txtPrice.Text); } if (invalid == 1) { MessageBox.Show(sb.ToString()); } else { cims.CARs.Add(car); cims.SaveChanges(); MessageBox.Show("1 Record Added"); } } FinalPage finPage = new FinalPage(); this.Close(); finPage.ShowDialog(); try { this.Show(); } catch { } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }