I am trying to build a small "application" that uses checkboxes as a search method. It works fine when only 1 box is checked at a time, but when I am checking more than one box, then it shows result from the last checked box only. I have been trying to debug it and I can see the datasource get overriden for every encounter. Anyone that can help with a input on how to "store" the data and not update it on every "if" statement?
C#
private void UpdateBinding()
{
wodListBox.DataSource = null;
wodListBox.DataSource = Exercise;
wodListBox.DisplayMember = "ExerciseID";
}
private void Checkboxes()
{
DataAccess db = new DataAccess();
if (BarBellCheckBox.Checked)
{
Exercise = db.GetExercise(BarBellCheckBox.Text);
}
if (WallBallCheckBox.Checked)
{
Exercise = db.GetExercise(WallBallCheckBox.Text);
}
if (KettleBellCheckBox.Checked)
{
Exercise = db.GetExercise(KettleBellCheckBox.Text);
}
if (BarCheckBox.Checked)
{
Exercise = db.GetExercise(BarCheckBox.Text);
}
UpdateBinding();
}
public class DataAccess
{
public List<ExerciseModel> GetExercise(string ExerciseEquipment)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("WodDB")))
{
return connection.Query<ExerciseModel>("dbo.GetEquipmentType @ExerciseEquipment", new { ExerciseEquipment = ExerciseEquipment }).ToList();
}
}
}
T SQL
CREATE PROCEDURE [dbo].[GetEquipmentType]
@ExerciseEquipment nvarchar(50)
AS
BEGIN
SELECT ExerciseName, ExerciseEquipment
FROM dbo.ExerciseName
INNER JOIN dbo.ExerciseEquipment
ON dbo.ExerciseName.ExerciseID = dbo.ExerciseEquipment.ExerciseID
WHERE ExerciseEquipment = @ExerciseEquipment
END
Quick: You are overriding the results in every if
-Block.
So how to fix this?
Since your 'Exercise' is of the type a List<ExerciseModel>
you could initialize it before your if
statements like:
Exercise = new List<ExerciseModel>();
and later on within your if block just use AddRange()
Exercise.AddRange( db.GetExercise(BarBellCheckBox.Text) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.