private async void buttonAddMultipleCars_Click(object sender, EventArgs e) { foreach (SqlCar car in pendingAddCars) { await SqlCar.CreateDatabaseEntryAsync(car); } }
public static SqlCar NewGumtreeCar(string html) { string gumtreeJsonDataLayer = ExtractGumtreeJSON(html); gumtreeJsonDataLayer = gumtreeJsonDataLayer.TrimStart('['); dynamic obj = JsonConvert.DeserializeObject(gumtreeJsonDataLayer); SqlCar gumtreeCar = new SqlCar(); gumtreeCar.id = -1; gumtreeCar.registration = obj.a.attr.vrn; gumtreeCar.make = obj.a.attr.vehicle_make; gumtreeCar.model = obj.a.attr.vehicle_model; gumtreeCar.trim = "N/A"; gumtreeCar.mileage = obj.a.attr.vehicle_mileage; gumtreeCar.colour = obj.a.attr.vehicle_colour; gumtreeCar.year = obj.a.attr.vehicle_registration_year; gumtreeCar.price = Convert.ToDecimal(obj.a.attr.price) / 100; gumtreeCar.url = ExtractViaRegex(html, "https://", " -->"); gumtreeCar.location = obj.l.pcid; gumtreeCar.dateAdded = DateTime.Today; gumtreeCar.mot = 0; return(gumtreeCar); }
public void AddGumtreeFiles() { listView2.Clear(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "HTML (*.html)|*.html|" + "All files (*.*)|*.*"; ofd.Multiselect = true; ofd.Title = "HTML file browser"; if (ofd.ShowDialog() == DialogResult.OK) { foreach (string file in ofd.FileNames) { string fullHtml = File.ReadAllText(file); SqlCar newCar = WebScraper.NewGumtreeCar(fullHtml); pendingAddCars.Add(newCar); ListViewItem newItem = new ListViewItem(newCar.id.ToString()); foreach (PropertyInfo prop in newCar.GetType().GetProperties()) { if (prop.Name != "id") { newItem.SubItems.Add(prop.GetValue(newCar, null).ToString()); } } listView2.Items.Add(newItem); } } }
// The method collects the text from the textboxes and sends it to be processed on the SqlCar class public void UpdateCar() { string[] textBoxStrings = { textBox_ID.Text, textBox_Reg.Text, textBox_Make.Text, textBox_Model.Text, textBox_Trim.Text, textBox_Mileage.Text, textBox_Colour.Text, textBox_Year.Text, textBox_Price.Text, textBox_URL.Text, textBox_Location.Text, dateTimePicker_DateAdded.Text, textBox_MOT.Text }; SqlCar.UpdateDatabaseEntry(textBoxStrings); }
public void DeleteCar() { DialogResult result = MessageBox.Show( "Are you sure you want to delete this entry?", "Important Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { SqlCar.DeleteDatabaseEntry(listView1.SelectedItems[0].SubItems[0].Text); } }
public async void CreateNewCar() { SqlCar newCar = new SqlCar(); newCar.registration = textBoxNewReg.Text.ToUpper().Replace(" ", ""); newCar.make = textBoxNewMake.Text; newCar.model = textBoxNewModel.Text; newCar.trim = textBoxNewTrim.Text; newCar.mileage = int.Parse(textBoxNewMileage.Text); newCar.colour = textBoxNewColour.Text; newCar.year = Decimal.ToInt32(numericUpDownYear.Value); newCar.price = decimal.Parse(textBoxNewPrice.Text); newCar.url = textBoxNewURL.Text; newCar.location = textBoxNewLocation.Text; newCar.dateAdded = DateTime.Today; newCar.mot = Decimal.ToInt32(numericUpDownMOT.Value); // TODO - Insert here: a check for duplicate cars via registration number before calling the DB await SqlCar.CreateDatabaseEntryAsync(newCar); }
// Add one car via manual method (but I am still going to add cars en masse with this method because I want to wrap this project up) public static async Task CreateDatabaseEntryAsync(SqlCar newCar) { List <string> columns = GetColumnNames(); columns.RemoveAt(0); // Remove id column as it is assigned automatically as opposed to if (str != "Id") string cmdStrMiddle = String.Join(",", columns.ToArray()); cmdStrMiddle.TrimEnd(','); List <string> carPropertiesNames = new List <string>(); foreach (PropertyInfo prop in newCar.GetType().GetProperties()) { if (prop.Name != "id") { carPropertiesNames.Add("@_" + prop.Name); } } string cmdStrEnd = String.Join(",", carPropertiesNames.ToArray()); cmdStrEnd.TrimEnd(','); await connect.OpenAsync(); string commandString = String.Format("INSERT INTO Car (" + cmdStrMiddle + ") VALUES (" + cmdStrEnd + ")"); SqlCommand command = new SqlCommand(commandString, connect); foreach (PropertyInfo prop in newCar.GetType().GetProperties()) { if (prop.Name != "id") { command.Parameters.AddWithValue("@_" + prop.Name, prop.GetValue(newCar, null)); } } await command.ExecuteNonQueryAsync(); connect.Close(); }
// Non-async method of querying database. Loops through the properties of an object via reflections, then put data into a ListView public async void DisplayAll() { myCars = await SqlCar.ReadDatabaseAsync("SELECT * FROM Car"); PopulateListView1(myCars); }