Sql to select the prerequisite of a certain course

  Uncategorised

I have a table that has courses details as follows :
Courses Table

and In the Windows form I placed a textBox and a button, when a students writes the name of the course to see the prerequisite requirement of that course as follows:
Windows Form

So I tried using the following code :

conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select Name from Courses where (Name = '" + textBox2.Text + "') and  Preq = Code";
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt); 
        dataGridView3.DataSource = dt;
        conn.Close();
        textBox2.Text = "";

but the code gives me the name column without any data as follows:

Result of my Code

Also is there a way to show a message when the course does not have any requirement such as " this course has no prerequisite requirement " something like that, and thank you.

Source: Visual Studio Questions

LEAVE A COMMENT