protected void Add_Click(object sender, EventArgs e) { Page.Validate(); if (!Page.IsValid) return; lblTruckExists.Visible = false; if (!DbInterfaceTruck.TruckExists(LicensePlateNum.Text, LicensePlateState.SelectedValue)) { Truck truck = new Truck(LicensePlateNum.Text, LicensePlateState.Text, Convert.ToInt32(Model.SelectedValue), Convert.ToDouble(FuelTankSize.Text), Convert.ToDouble(Odometer.Text), Convert.ToDouble(Length.Text), Convert.ToDouble(Width.Text), Convert.ToDouble(Height.Text), Convert.ToDouble(Weight.Text), Remarks.Text, LocalTruck.Checked); if (truck.AddTruckToDatabase()) { MainForm.Visible = false; Confirmation.Visible = true; } } else lblTruckExists.Visible = true; }
/* Pre: * Post: Retrieves a list of all idle trucks * @returns a list of available trucks */ public static List<Truck> GetTrucksByStatus(UtilityClass.TruckStatus status) { List<Truck> trucks = new List<Truck>(); DataTable table = new DataTable(); SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["GoodsDeliveryConnectionString"].ConnectionString); try { connection.Open(); string storedProc = "TruckSelectByStatus"; SqlCommand cmd = new SqlCommand(storedProc, connection); SqlDataAdapter adapter = new SqlDataAdapter(cmd); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@statusId", status); adapter.Fill(table); for (int i = 0; i < table.Rows.Count; i++) { int id = Convert.ToInt32(table.Rows[i]["Id"]); string licenseNum = table.Rows[i]["LicensePlateNum"].ToString(); string licenseState = table.Rows[i]["LicensePlateState"].ToString(); double length = Convert.ToDouble(table.Rows[i]["Length"]); double width = Convert.ToDouble(table.Rows[i]["Width"]); double height = Convert.ToDouble(table.Rows[i]["Height"]); double weight = Convert.ToDouble(table.Rows[i]["MaxWeight"]); int modelId = Convert.ToInt32(table.Rows[i]["ModelId"]); double fuelTankSize = Convert.ToDouble(table.Rows[i]["FuelTankSize"]); double odometer = Convert.ToDouble(table.Rows[i]["OdometerReading"]); string remark = table.Rows[i]["Remark"].ToString(); //find if truck is for local deliveries bool local = false; if (!table.Rows[i]["LocalTruck"].ToString().Equals("")) local = Convert.ToBoolean(table.Rows[i]["LocalTruck"]); Truck truck = new Truck(id, licenseNum, licenseState, modelId, fuelTankSize, odometer, length, width, height, weight, remark, local); truck.status = status; trucks.Add(truck); } } catch (Exception e) { trucks = null; } connection.Close(); return trucks; }
/* Pre: * Post: Retrieves a list of all idle trucks * @returns a list of available trucks */ public static Truck GetTruckById(int id) { Truck truck = null; DataTable table = new DataTable(); SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["GoodsDeliveryConnectionString"].ConnectionString); try { connection.Open(); string storedProc = "TruckSelectById"; SqlCommand cmd = new SqlCommand(storedProc, connection); SqlDataAdapter adapter = new SqlDataAdapter(cmd); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@truckId", id); adapter.Fill(table); if (table.Rows.Count == 1) { string licenseNum = table.Rows[0]["LicensePlateNum"].ToString(); string licenseState = table.Rows[0]["LicensePlateState"].ToString(); double length = Convert.ToDouble(table.Rows[0]["Length"]); double width = Convert.ToDouble(table.Rows[0]["Width"]); double height = Convert.ToDouble(table.Rows[0]["Height"]); double weight = Convert.ToDouble(table.Rows[0]["MaxWeight"]); int modelId = Convert.ToInt32(table.Rows[0]["ModelId"]); double fuelTankSize = Convert.ToDouble(table.Rows[0]["FuelTankSize"]); double odometer = Convert.ToDouble(table.Rows[0]["OdometerReading"]); string remark = table.Rows[0]["Remark"].ToString(); int status = Convert.ToInt32(table.Rows[0]["StatusId"]); //find if truck is for local deliveries bool local = false; if (!table.Rows[0]["LocalTruck"].ToString().Equals("")) local = Convert.ToBoolean(table.Rows[0]["LocalTruck"]); int pathId = -1; if (!table.Rows[0]["PathId"].ToString().Equals("")) pathId = Convert.ToInt32(table.Rows[0]["PathId"]); truck = new Truck(id, licenseNum, licenseState, modelId, fuelTankSize, odometer, length, width, height, weight, remark, local); truck.status = (UtilityClass.TruckStatus)status; if (pathId != -1) truck.pathId = pathId; } } catch (Exception e) { truck = null; } connection.Close(); return truck; }