I am trying to store multiple lists to a database but I'm struggling on finding a better way of doing it, as the way I am doing it now, seems to be incorrect.
On my form, I have 6 different legs. Each leg consists of Cargo Weight, Bag Weight, Fuel Weight, and more.
Right now I am storing all the data to a new class called FlightDetails, where that class just has properties, no methods. For each leg, I am having to re-set all the property details, but for a different leg as seen below:
private void InsertLeg1()
{
List<FlightDetails> leg1 = new List<FlightDetails>();
leg1.Add(new FlightDetails
{
Date = dateTimeLoadPlanner.Value.Date,
FlightNumber = cbFlightNumber.Text,
LegNumber = 1,
PassengerWeight = Convert.ToInt32(tbPax_L1.Text),
BagWeight = Convert.ToInt32(tbBags_L1.Text),
CargoWeight = Convert.ToInt32(tbCargo_L1.Text),
Equipment = Convert.ToInt32(tbEquip_L1.Text),
AWI = Convert.ToInt32(tbAWI_L1.Text),
Fuel = Convert.ToInt32(tbFuel_L1.Text),
Contigency = Convert.ToInt32(tbCont_L1.Text),
TaxiBurn = Convert.ToInt32(tbTaxiBurn_L1.Text),
TakeOffWeight = Convert.ToInt32(tbMTOW_L1.Text),
LandingWeight = Convert.ToInt32(tbLandingWT_L1.Text),
PassengerNumber = Convert.ToInt32(tbPaxNo_L1.Text),
Seatpacks = Convert.ToInt32(tbSeatpacks_L1.Text),
Aircraft = cbAircraft.Text,
AircraftType = cbAircarftType.Text,
Crew = cbCrew.Text,
Notes = tbNotesALC.Text,
AircraftWeight = Convert.ToInt32(tbAircraftWeight.Text),
FuelBurn = Convert.ToInt32(tbFuelBurn_L1.Text),
AircraftConfiguration = tbSeats_L1.Text
});
SaveDataALC.SaveLeg(leg1);
}
private void InsertLeg2()
{
List<FlightDetails> leg2 = new List<FlightDetails>();
leg2.Add(new FlightDetails
{
Date = dateTimeLoadPlanner.Value.Date,
FlightNumber = cbFlightNumber.Text,
LegNumber = 2,
PassengerWeight = Convert.ToInt32(tbPax_L2.Text),
BagWeight = Convert.ToInt32(tbBags_L2.Text),
CargoWeight = Convert.ToInt32(tbCargo_L2.Text),
Equipment = Convert.ToInt32(tbEquip_L2.Text),
AWI = Convert.ToInt32(tbAWI_L2.Text),
Fuel = Convert.ToInt32(tbFuel_L2.Text),
Contigency = Convert.ToInt32(tbCont_L2.Text),
TaxiBurn = Convert.ToInt32(tbTaxiBurn_L2.Text),
TakeOffWeight = Convert.ToInt32(tbMTOW_L2.Text),
LandingWeight = Convert.ToInt32(tbLandingWT_L2.Text),
PassengerNumber = Convert.ToInt32(tbPaxNo_L2.Text),
Seatpacks = Convert.ToInt32(tbSeatpacks_L2.Text),
Aircraft = cbAircraft.Text,
AircraftType = cbAircarftType.Text,
Crew = cbCrew.Text,
Notes = tbNotesALC.Text,
AircraftWeight = Convert.ToInt32(tbAircraftWeight.Text),
FuelBurn = Convert.ToInt32(tbFuelBurn_L2.Text),
AircraftConfiguration = tbSeats_L2.Text
});
SaveDataALC.SaveLeg(leg2);
}
public void InsertLeg3()
{
List<FlightDetails> leg3 = new List<FlightDetails>();
leg3.Add(new FlightDetails
{
Date = dateTimeLoadPlanner.Value.Date,
FlightNumber = cbFlightNumber.Text,
LegNumber = 3,
PassengerWeight = Convert.ToInt32(tbPax_L3.Text),
BagWeight = Convert.ToInt32(tbBags_L3.Text),
CargoWeight = Convert.ToInt32(tbCargo_L3.Text),
Equipment = Convert.ToInt32(tbEquip_L3.Text),
AWI = Convert.ToInt32(tbAWI_L3.Text),
Fuel = Convert.ToInt32(tbFuel_L3.Text),
Contigency = Convert.ToInt32(tbCont_L3.Text),
TaxiBurn = Convert.ToInt32(tbTaxiBurn_L3.Text),
TakeOffWeight = Convert.ToInt32(tbMTOW_L3.Text),
LandingWeight = Convert.ToInt32(tbLandingWT_L3.Text),
PassengerNumber = Convert.ToInt32(tbPaxNo_L3.Text),
Seatpacks = Convert.ToInt32(tbSeatpacks_L3.Text),
Aircraft = cbAircraft.Text,
AircraftType = cbAircarftType.Text,
Crew = cbCrew.Text,
Notes = tbNotesALC.Text,
AircraftWeight = Convert.ToInt32(tbAircraftWeight.Text),
FuelBurn = Convert.ToInt32(tbFuelBurn_L3.Text),
AircraftConfiguration = tbSeats_L3.Text
});
SaveDataALC.SaveLeg(leg3);
}
Once that information is passed, I am storing it in the database here:
public static void SaveLeg(List<FlightDetails> legDetails)
{
using (SqlConnection conn = new SqlConnection(ConnectionLoader.ConnectionString("Threshold")))
{
foreach (var legDetail in legDetails)
{
conn.Open();
SqlCommand saveData = new SqlCommand("Save_Leg_Loadplanner", conn);
saveData.CommandType = CommandType.StoredProcedure;
saveData.Parameters.AddWithValue("@DateID", legDetail.Date);
saveData.Parameters.AddWithValue("@FlightNumber", legDetail.FlightNumber);
saveData.Parameters.AddWithValue("@LegNumber", legDetail.LegNumber);
saveData.Parameters.AddWithValue("@PassengerWeight", legDetail.PassengerWeight);
saveData.Parameters.AddWithValue("@BagWeight", legDetail.BagWeight);
saveData.Parameters.AddWithValue("@CargoWeight", legDetail.CargoWeight);
saveData.Parameters.AddWithValue("@Equipment", legDetail.Equipment);
saveData.Parameters.AddWithValue("@AWI", legDetail.AWI);
saveData.Parameters.AddWithValue("@Fuel", legDetail.Fuel);
saveData.Parameters.AddWithValue("@TaxiBurn", legDetail.TaxiBurn);
saveData.Parameters.AddWithValue("@Contigency", legDetail.Contigency);
saveData.Parameters.AddWithValue("@TakeOffWeight", legDetail.TakeOffWeight);
saveData.Parameters.AddWithValue("@LandingWeight", legDetail.LandingWeight);
saveData.Parameters.AddWithValue("@PassengerNumber", legDetail.PassengerNumber);
saveData.Parameters.AddWithValue("@Seatpacks", legDetail.Seatpacks);
saveData.Parameters.AddWithValue("@Aircraft", legDetail.Aircraft);
saveData.Parameters.AddWithValue("@Notes", legDetail.Notes);
saveData.Parameters.AddWithValue("@Crew", legDetail.Crew);
saveData.Parameters.AddWithValue("@AircraftType", legDetail.AircraftType);
saveData.Parameters.AddWithValue("@AircraftWeight", legDetail.AircraftWeight);
saveData.Parameters.AddWithValue("@FuelBurn", legDetail.FuelBurn);
saveData.Parameters.AddWithValue("@AircraftConfiguration", legDetail.AircraftConfiguration);
saveData.ExecuteNonQuery();
}
}
}
I'm not sure if there is a better way of doing this. This does work, but I feel there may be a more cleaner way of doing this.