ComboBox shows System.Data.DataRowView first

71 Views Asked by At

So in windowsform I have 2 ComboBoxes and one CheckedListBox for exercises. When you select your typefitness (calisthenics, weights, cardio, etc.) and your musclegroup (tricep, chest, back, forearms, etc.) than you should get the exercises out of the CheckedListBox.

Both ComboBoxes' default values are 1, so it should show something like pushups in the CheckedListBox, because TypeFitness = 1 is Calisthenics and MuscleGroup = 1 is Front Shoulder.

Instead it shows System.Data.DataRowView, but the second time I select Calisthenics in the form it shows the right thing. Is there a way to make it so it shows the exercises immediately?

(System.Data.DataRowView only shows when the previous selection didn't have an outcome)

Code:

private void Exercises()
{
    string query = "SELECT X.ExerciseId, X.Naam FROM Xercises AS X " +
            "INNER JOIN MG_Exercise AS MGX ON MGX.ExerciseId = X.ExerciseId " +
            "WHERE MGX.MuscleId = @MuscleId AND X.FitnessId = @FitnessId";

    using (connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(query, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        SqlParameter param = command.Parameters.AddWithValue("@MuscleId", comBoxMuscleGroup.SelectedValue);
        SqlParameter param2 = command.Parameters.AddWithValue("@FitnessId", comBoxTypeFitness.SelectedValue);

        if(param.Value == null)
        {
            param.Value = DBNull.Value;
        }
        if(param2.Value == null)
        {
            param2.Value = DBNull.Value;
        }

        DataTable Xdata = new DataTable();
        adapter.Fill(Xdata);

        clbXcercises.DisplayMember = "Naam";
        clbXcercises.ValueMember = "X.ExerciseId";
        clbXcercises.DataSource = Xdata;
    }
}
1

There are 1 best solutions below

0
On

The problem was that I put DataSource last Exercises(). So it should be:

clbXcercises.DataSource = Xdata;
clbXcercises.DisplayMember = "Naam";
clbXcercises.ValueMember = "X.ExerciseId";