0

The error is 'No mapping exists from object type System.Data.DataRowView to a known managed provider native type.' Now comBoxMuscleGroup.SelectedValue and comBoxTypeFitness.SelectedValue should return Id's but instead they return 'System.Data.DataRowView'. Can anybody help me out?

code:

       private void TypeFitness()
       {
           string query = "SELECT Naam FROM TypeFitness";

           using (connection = new SqlConnection(connectionString))
           using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
           {
               DataTable data = new DataTable();
               adapter.Fill(data);

               comBoxTypeFitness.DisplayMember = "Naam";
               comBoxTypeFitness.ValueMember = "FitnessId";
               comBoxTypeFitness.DataSource = data;
           }
       }

       private void MuscleGroup()
       {
           string query = "SELECT Naam FROM MuscleGroup";

           using (connection = new SqlConnection(connectionString))
           using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
           {
               DataTable data = new DataTable();
               adapter.Fill(data);

               comBoxMuscleGroup.DisplayMember = "Naam";
               comBoxMuscleGroup.ValueMember = "MuscleId";
               comBoxMuscleGroup.DataSource = data;
           }
       }

       private void Exercises()
       {
           string query = "SELECT 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))
           {
               //int MuscleId = ((DataRowView)comBoxMuscleGroup.SelectedValue).Row.Field<int>("MuscleId");
            //int FitnessId = ((DataRowView)comBoxTypeFitness.SelectedValue).Row.Field<int>("FitnessId");

               command.Parameters.AddWithValue("@MuscleId", comBoxMuscleGroup.SelectedValue);
               command.Parameters.AddWithValue("@FitnessId", comBoxTypeFitness.SelectedValue);

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

               clbXcercises.DisplayMember = "Naam";
               clbXcercises.ValueMember = "ExerciseId";
               clbXcercises.DataSource = data;
           }

       }
zhrgci
  • 584
  • 1
  • 6
  • 25
  • You don't have any ID in your queries. Just Naam. How is it supposed to return the id if you don't provide it when you load the combos? – Steve Feb 28 '19 at 13:52
  • Isn't `comBoxTypeFitness.ValueMember = "FitnessId"` giving it a value? – zhrgci Feb 28 '19 at 13:54
  • 1
    No because it could only return values if you give it (the combo) an id value from your db. You need to add it to your queries – Steve Feb 28 '19 at 13:54
  • 1
    _string query = "SELECT MuscleID, Naam FROM MuscleGroup";_ and so on on the other two queries (also the execises combo requires it) – Steve Feb 28 '19 at 13:56
  • Thank you! I have a smaller second problem though. It works, but in the beginning and some times after it shows `System.Data.DataRowView` first and when i click again, it shows the right stuff. Any idea? – zhrgci Feb 28 '19 at 14:01

1 Answers1

2

The properties ValueMember and DisplayMember are two strings that should be the name of two fields that provides the values from the DataSource.
Your queries don't contains the fields that you have named as ValueMember for your combos.
So it is not possible for it to give a precise value in the property SelectedValue but just the name of the class that is used to build a row inside the combo (a DataRowView)

If you want to get the SelectedValue set to the MuscleID or FitnessID value of the current selected item you need extract these values from the database.

You need to change your queries to

string query = "SELECT FitnessID, Naam FROM TypeFitness";

and

string query = "SELECT MuscleID, Naam FROM MuscleGroup";

also the final query needs to have the exerciseID

  string query = "SELECT ExerciseID, Naam FROM Xercises AS X " +
       "INNER JOIN MG_Exercise AS MGX ON MGX.ExerciseId = X.ExerciseId " +
       "WHERE MGX.MuscleId = @MuscleId AND X.FitnessId = @FitnessId";

I suggest also to always check for null against the SelectedValue property before using it.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you! You solved 2 problems from one without even looking at the question! That last sentence did the trick on my second smaller problem. :D – zhrgci Feb 28 '19 at 14:05