Results 1 to 3 of 3

Thread: School Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    1

    School Program

    I am working on a school program im stuck at a point where I have a button and i want it to get the Highest grade of a certain Exam , i used the below code but didnt work i want the button to return a messagebox with the student name and exam name and grade which is the highest one, and to specify which exam im using a combobox with the list of exam names , noting that im using SQL management studio for database information and in the screenshot is the form design

    Code:
    SqlCommand cmd = new SqlCommand("SELECT COUNT(*) AS Expr1, ExamName, StudentName, MAX(Grade) AS Expr2 FROM Grades WHERE ExamName = @ex GROUP BY ExamName, StudentName, Grade ORDER BY Grade DESC", conn);
                cmd.Parameters.AddWithValue("@ex", cbExam.SelectedValue.ToString());
                conn.Open();
                SqlDataReader rd = cmd.ExecuteReader();
                string outputMessage = "";
                try
                {
                    while (rd.Read())
                    {
                        outputMessage += rd.GetValue(2) + "- " + rd.GetValue(3) + "\n";
                    }
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message.ToString());
                }
    
                rd.Close();
                conn.Close();
                MessageBox.Show(outputMessage);
    Attached Images Attached Images  

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: School Program

    At the moment you're only passing in the exam name for a parameter. So the query will return the max grade for that exam - but for ALL students. So what you're seeing is probably just the first record, which would be teh student with the highest grade for that exam (since you have an order by on it). If you only want the highest grade for one student for one exam, then you need to either include the name in the where clause, in addition to the exam, or loop through the results to find the name you're looking for.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    New Member
    Join Date
    Jul 2022
    Posts
    1

    Re: School Program

    It's all very similar to my code learning, I studied html and javascript and it took me quite a long time to figure out some of the functions. I was never very good at programming so it was hard for me at first, but after going through the classes, I gradually mastered those markup languages. So, I wish you success! Sometimes it can seem overwhelmingly difficult, but the main thing is not to give up. Then I realized that I wanted to work in another field so I went to asa college miami florida for a second degree and studied medicine, which I always liked much more than programming. But my knowledge of code came in handy anyway.
    Last edited by kalonmarko; Jul 28th, 2022 at 09:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width