0

我收到一个错误

参数化查询“(@MuscleId int,@FitnessId nvarchar(4000))SELECT ExerciseId, Naam”需要参数“@FitnessId”,但未提供该参数。

我试图用parameter.Value = DBNull.Value. 但我仍然得到同样的错误,因为我可能做错了。

有人可以告诉我我做错了什么吗?

MGX.MuscleId并且X.FitnessId都设置为int)

代码:

private void Exercises()
{
    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";

    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);    *****ERROR*****

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

0 回答 0