using System; using System.Collections.Generic; using System.Text; using System.Data.SqlServerCe; using System.Data; using System.Collections; namespace CrimeScene.Logic { class Vehicle : Table { // Constructors public Vehicle(SqlCeConnection conn, SqlCeDataAdapter dataAdapter, DataTable dataTable, Action action) : base(conn, dataAdapter, dataTable, action) { this.exceptionMsg = "Exception message"; } protected override void dataAdapter_RowUpdated(object sender, SqlCeRowUpdatedEventArgs e) { if (this.selectedRecord < 0) { this.selectedRecord = this.dataTable.Rows.Count - 1; SqlCeCommand cmd = new SqlCeCommand("SELECT MAX(VEHICLE_ID) FROM VEHICLE", this.conn); this.primaryKey = (Int32)(Int64)cmd.ExecuteScalar(); this.dataTable.Rows[this.selectedRecord]["VEHICLE_ID"] = this.primaryKey; } } public void Save(int incidentId, string registration, string make, string model, int sceneId, int userId) { string valueString = "registration: " + registration + ", make: " + make + ", model: " + model; if (!IsNumeric(incidentId.ToString())) { throw new DataException(this.exceptionMsg); } else { if (this.selectedRecord >= 0) { // Overwrite DataRow row = this.dataTable.Rows[this.selectedRecord]; row["INCIDENT_ID"] = incidentId; row["REGISTRATION"] = registration; row["MAKE"] = make; row["MODEL"] = model; this.action.Save(sceneId, userId, "Table VEHICLE updated: " + valueString); } else { // Add this.dataTable.Rows.Add(new object[] { null, incidentId, registration, make, model, (int)IdType.Unknown }); this.action.Save(sceneId, userId, "Table VEHICLE inserted: " + valueString); } try { this.dataAdapter.Update(this.dataTable); } catch (DBConcurrencyException ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } } public override int Id() { if (Open()) { return Id(this.selectedRecord); } return (int)IdType.Unknown; } public int Id(int i) { return (Int32)this.dataTable.Rows[i]["VEHICLE_ID"]; } public int IncidentId() { if (Open()) { return (Int32)this.dataTable.Rows[this.selectedRecord]["INCIDENT_ID"]; } return (int)IdType.Unknown; } public int IncidentId(int i) { return (Int32)this.dataTable.Rows[i]["INCIDENT_ID"]; } public string Registration() { if (Open()) { return Registration(this.selectedRecord); } return ""; } public string Registration(int i) { return (string)this.dataTable.Rows[i]["REGISTRATION"]; } public string Make() { if (Open()) { return (string)this.dataTable.Rows[this.selectedRecord]["MAKE"]; } return ""; } public string Model() { if (Open()) { return (string)this.dataTable.Rows[this.selectedRecord]["MODEL"]; } return ""; } public string[] InicdentVehicleList(int incidentId) { ArrayList result = new ArrayList(); int listIndex = -1; this.mapper.clearMapping(); for (int i = 0; i < Count(); i++) { if (incidentId == IncidentId(i)) { this.mapper.createMapping(++listIndex, i); result.Add(Registration(i)); } } return result.ToArray(typeof(string)) as string[]; } } }