user2469932
user2469932

Reputation: 167

Why is my SELECT repeating with dataGrid?

 public static DataSet selectStudent()
 {
      MySqlConnection conn = connection();
      conn.Open();
      MySqlCommand cmd = new MySqlCommand();
      cmd.Connection = conn;
      MySqlDataAdapter adap = new MySqlDataAdapter(@"SELECT person.*, student.gradePointAverage, student.majorField FROM person JOIN student", conn);
      MySqlCommandBuilder sqlCmd = new MySqlCommandBuilder(adap);
      DataSet sqlSet = new DataSet();
      adap.Fill (sqlSet, "studentInfo");
      conn.Close();
      return sqlSet;
 }

and the button:

 private void btnAdminStudentView_Click(object sender, EventArgs e)
 {
    DataSet ds = studentHelperClass.selectStudent();
    dataGridStudent.DataSource = ds.Tables["studentInfo"];
 }

why would this be giving me a result like this on the button click?

enter image description here

Upvotes: 4

Views: 56

Answers (1)

John Woo
John Woo

Reputation: 263703

Your query produces cartesian product because you failed to define how the records are related with each other. You need to add a condition,

SELECT person.*, student.gradePointAverage, student.majorField 
FROM person JOIN student
     ON person.ID = student.ID  // example only

this means that records from table person are related with ID on table student.

To further gain more knowledge about joins, visit the link below:

Upvotes: 6

Related Questions