The application consists of displaying data from a SQL Server DB where there are two tables: one for students and the other for grades that are linked by the ID of the students. In a Windows Form there is a combobox that shows the students and I want it to show me the notes associated with the selected student in a listbox. I have managed to show the data in the listbox from the first selection of the combobox but when changing the selection of the combobox the associated data of the new student selected in the combobox does not change:
As can be seen when changing the student, it does not show the notes associated with the new student but continues to show those of the first. The code I have used is the following:
public partial class AgregarNotas : Form
{
SqlConnection conexion = new SqlConnection("server = DESKTOP-B1MVQBF; database = Formacion; integrated security = true");
public AgregarNotas()
{
InitializeComponent();
}
private void AgregarNotas_Load(object sender, EventArgs e)
{
MuestraAlumnos();
MuestraNotas();
}
private void MuestraAlumnos()
{
conexion.Open();
string consulta = "select IdAlumnos, concat(IdAlumnos, ' - ', NombreAlumno, ' - ', ApellidoAlumno, ' - ', TlfAlumno, ' - ', EmailAlumno, ' - ', BajaAlumno) as info from Alumnos where bajaAlumno = 0;";
SqlCommand comando = new SqlCommand(consulta, conexion);
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
DataTable tablaAlumnos = new DataTable();
adaptador.Fill(tablaAlumnos);
cbxAddNotas.DataSource = tablaAlumnos;
cbxAddNotas.DisplayMember = "info";
cbxAddNotas.ValueMember = "IdAlumnos";
conexion.Close();
}
private void MuestraNotas()
{
conexion.Open();
string consulta = "select Nota from Notas where IdAlumno = @Id;";
SqlCommand comando = new SqlCommand(consulta, conexion);
comando.Parameters.AddWithValue("@Id", Convert.ToInt32(cbxAddNotas.SelectedValue));
SqlDataAdapter adaptador = new SqlDataAdapter(comando);
DataTable tablaAlumnos = new DataTable();
adaptador.Fill(tablaAlumnos);
ltbNotaAlumno.DataSource = tablaAlumnos;
ltbNotaAlumno.DisplayMember = "Nota";
conexion.Close();
}
How can I make it so that when I change the student in the combobox, it shows me the notes associated with that student in the listbox so that I can then select them and edit the notes later?
I add in case it is of interest how are the tables of the SQL Server database
If by adding the SelectedIndexChanged event to the combobox and applying the method MuestraNotas()
inside
private void cbxAddNotas_SelectedIndexChanged(object sender, EventArgs e)
{
MuestraNotas();
}
you get the following exception:
To check that cbxAddNotas.SelectecValue was not empty or without a value, I created a variable:
int prueba = (int)cbxAddNotas.SelectedValue;
label1.Text = prueba.ToString();
When I select the student with Id 2 in the combobox, the number 2 appears in the label:
when I change in the combobox to student 3 in the label the number 2 continues to appear. With which it must be a problem related to changing in the combobox.
If you double-click the combobox in the design section of the form, it generates the code for an event of type SelectedIndexChanged.
Inside it, enter the following code:
The if conditional checks when the index of the combobox selection changes and executes your ShowNotes() function.
The conditional else if is used for the moment in which you load the form, where an index change is registered, which is 0 by default. Closing the connection is necessary so that an error does not occur, but I do not know exactly what causes it .
I hope it helps you, greetings.