C#
C#

help

Root Question Message

rober
rober2/27/2023
❔ ✅ How to call a specific attribute from a getter class to another class

Hey there, I am struggling to call this attribute from another class. The two relevant code pieces are:

public List<CompletedAssignment> GetCompletedAssignments(int studentID, int assignmentID)
        {
            List<CompletedAssignment> completedAssignments = new List<CompletedAssignment>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string query = "SELECT StudentID, AssignmentID, EarnedPoints FROM CompletedAssignments WHERE StudentID = @StudentID AND AssignmentID = @AssignmentID";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@StudentID", studentID);
                command.Parameters.AddWithValue("@AssignmentID", assignmentID);

                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        CompletedAssignment completedAssignment = new CompletedAssignment();

                        completedAssignment.StudentID = reader.GetInt32(0);
                        completedAssignment.AssignmentID = reader.GetInt32(1);
                        completedAssignment.EarnedPoints = reader.GetInt32(2);

                        completedAssignments.Add(completedAssignment);
                    }
                }
            }

            return completedAssignments;
        }



and
rober
rober2/27/2023
        private void UpdateTextBoxes()
        {
            if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
            {
                return;
            }

            Student student = (Student)comboBox1.SelectedItem;
            Assignment assignment = (Assignment)comboBox2.SelectedItem;

            string completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID).ToString();

            textBox1.Text = completedAssignment;
            textBox2.Text = assignment.TotalPoints.ToString();
        }


the issue is that I cant get the specific attribute I need into this textbox
Ero
Ero2/27/2023
What attribute?
rober
rober2/27/2023
sorry
rober
rober2/27/2023
the EarnedPoints attribute
rober
rober2/27/2023
what I want to do is this:
        private void UpdateTextBoxes()
        {
            if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
            {
                return;
            }

            Student student = (Student)comboBox1.SelectedItem;
            Assignment assignment = (Assignment)comboBox2.SelectedItem;


            List<CompletedAssignment> completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID);


            textBox1.Text = completedAssignment.EarnedPoints.ToString();

            textBox2.Text = assignment.TotalPoints.ToString();
        }

    }
}
`
rober
rober2/27/2023
but it throws the error
rober
rober2/27/2023
and I am not sure how to grab just that attribute from the list
Ero
Ero2/27/2023
So, those are called properties, not attributes
Ero
Ero2/27/2023
Attributes are very specifically different things in C#
rober
rober2/27/2023
ok sorry
rober
rober2/27/2023
I am just unsure of how to display that property
Ero
Ero2/27/2023
You have an entire list of CompletedAssignments
Ero
Ero2/27/2023
You kinda need to choose just one to get its EarnedPoints
Ero
Ero2/27/2023
How you choose which one to take... that'll have to be up to you
rober
rober2/27/2023
so like
rober
rober2/27/2023
hmmm
rober
rober2/27/2023
look I am feeding it a studentID and assignmentID right
rober
rober2/27/2023
and its supposed to output the EarnedPoints value
rober
rober2/27/2023
how do I choose in the list though?
Ero
Ero2/27/2023
I don't know
Ero
Ero2/27/2023
It's your list
Ero
Ero2/27/2023
By the way you call GetCompletedAssignments wrong
Ero
Ero2/27/2023
You have student ID and assignment ID backwards in your call
rober
rober2/27/2023
thanks for the help! :) jk!
rober
rober2/27/2023
!close
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy