Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner programmer developing a C# WinForms solution in VS 2015.

I was successful in cascading two out of three ComboBoxes. The problem is with the third ComboBox, which doesn't display the correct filtered values. It always displays the same values.

Can you please take a look at my code and tell me what's wrong? I really appreciate your time and help. Thank you!

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Bremington
{
    public partial class Form1 : Form
    {

        SqlConnection con = new SqlConnection(@"Data Source=LAPTOP-C30IHOU2;Initial Catalog=BremingtonBackEnd;Integrated Security=True");
        DataRow dr;

        public Form1()
        {
            InitializeComponent();
            refreshCurso();
        }

        public void refreshCurso()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from cursos", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox1.ValueMember = "cod_curso";
            comboBox1.DisplayMember = "curso";
            comboBox1.DataSource = dt;
        }

        public void refreshModulo(int cod_curso)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from modulos where cod_curso= @cod_curso", con);
            cmd.Parameters.AddWithValue("cod_curso", cod_curso);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox2.ValueMember = "cod_modulo";
            comboBox2.DisplayMember = "modulo";
            comboBox2.DataSource = dt;
        }

        public void refreshTurma(int cod_modulo)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from turmas where cod_modulo= @cod_modulo", con);
            cmd.Parameters.AddWithValue("cod_modulo", cod_modulo);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox3.ValueMember = "cod_turma";
            comboBox3.DisplayMember = "turma";
            comboBox3.DataSource = dt;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex.ToString() != null)
            {
                int cod_curso = Convert.ToInt32(comboBox1.SelectedIndex.ToString());
                refreshModulo(cod_curso);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex.ToString() != null)
            {
                int cod_modulo = Convert.ToInt32(comboBox2.SelectedIndex.ToString());
                refreshTurma(cod_modulo);
            }
        }

    }
}
Posted
Updated 11-Oct-16 8:40am
Comments
Richard MacCutchan 11-Oct-16 4:53am    
You need to use the debugger to find out what is happening.
[no name] 11-Oct-16 6:10am    
Yes you need to learn how to use the debugger and start thinking about what it is that you are trying to do. For example, Why are you converting an integer to string only to convert it back to an integer? Does that make sense?
José Amílcar Casimiro 11-Oct-16 6:12am    
Can you add the model database?
It seems to me you have a problem with the data model.

1 solution

This is working code, please have a look.

C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            refreshCurso();

        }
        
        SqlConnection con = new SqlConnection(@"Data Source=mymachine\SQLEXPRESS;Initial Catalog=MyTestDB;Integrated Security=True");
        DataRow dr;

        public void refreshCurso()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select  * from curso", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox1.ValueMember = "cod_curso";
            comboBox1.DisplayMember = "curso";
            comboBox1.DataSource = dt;
        }

        public void refreshModulo(int cod_curso)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select* from modulo where cod_curso = @cod_curso", con);
            cmd.Parameters.AddWithValue("cod_curso", cod_curso);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox2.ValueMember = "cod_modulo";
            comboBox2.DisplayMember = "modulo";
            comboBox2.DataSource = dt;
        }

        public void refreshTurma(int cod_modulo)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from turma where cod_modulo = @cod_modulo", con);
            cmd.Parameters.AddWithValue("cod_modulo", cod_modulo);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox3.ValueMember = "cod_turma";
            comboBox3.DisplayMember = "turma";
            comboBox3.DataSource = dt;
        }

      
        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue.ToString() != "")
            {
                int cod_curso = Convert.ToInt32(comboBox1.SelectedValue.ToString());
                refreshModulo(cod_curso);
            }
        }

        private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (comboBox2.SelectedValue.ToString() != "")
            {
                int cod_modulo = Convert.ToInt32(comboBox2.SelectedValue.ToString());
                refreshTurma(cod_modulo);
            }
        }
              
    }
}
 
Share this answer
 
Comments
JC Carmo 11-Oct-16 16:42pm    
Hi,Manu! Thank you very much for your help! Problem solved! YOur code worked perfectly! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900