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;
}
}
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.