Forums - C# Corner

Forum guidelines
Khenne Enyieko

Khenne Enyieko

  • 1.4k
  • 6
  • 4.2k

Binding data from database to a combobox

Mar 16 2013 6:40 AM

Good Day,
I am binding a data from MSSQL database to WPF combobox1 using C#. what i want to do is when i get the first data from the database which is CATEGORY i choose the category i want then i want the category i choose to be binded to another combobox2 which will give me the SUBCATEGORIES from the database as a result of choosing from the category. so far i have been able to get the category from the database to display on the combobox, my challenge is how to get the value from the category selected and using it to get the subcategories from the database.
below is part of my code
  public Register()
        {
            InitializeComponent();
            BindComboBox(cmbcategory);
            getComboName(cmbsubcategory);
          
        }

        public void BindComboBox(ComboBox comboBoxName)
        {
            string connectionString1 = "Data Source=Kels-PC;Initial Catalog=AGdb; User ID=sa; Password=esther;";
            SqlConnection con = new SqlConnection(connectionString1);
           // con.Open();
            //string com = "Select Nametab, Passwordtab from PasswordDB";
          //  SqlCommand cmdo = new SqlCommand(com, con);

           // SqlConnection conn = new SqlConnection("your connection string");
            SqlDataAdapter da = new SqlDataAdapter("Select CategoryName FROM Categorydb", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Categorydb");
            comboBoxName.ItemsSource = ds.Tables[0].DefaultView;
            comboBoxName.DisplayMemberPath = ds.Tables[0].Columns["CategoryName"].ToString();
           // comboBoxName.SelectedValuePath = ds.Tables[0].Columns["ZoneId"].ToString();
        }

        public void getComboName(ComboBox cbName)
        {
            object getcombo = cmbcategory.Text;
            string getcmb = getcombo.ToString();
           // string getcmb = cmbcategory.Text.ToString();
            string connectionString1 = "Data Source=Kels-PC;Initial Catalog=AGdb; User ID=sa; Password=esther;";
            SqlConnection con = new SqlConnection(connectionString1);
            // con.Open();
            //string com = "Select Nametab, Passwordtab from PasswordDB";
            //  SqlCommand cmdo = new SqlCommand(com, con);

            // SqlConnection conn = new SqlConnection("your connection string");
            SqlDataAdapter da = new SqlDataAdapter("Select SubName FROM SubCategory WHERE CategoryName='"+getcmb+"'", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "SubCategory");
            cbName.ItemsSource = ds.Tables[0].DefaultView;
            cbName.DisplayMemberPath = ds.Tables[0].Columns["SubName"].ToString();
        }
please am still new to this but your help will count alot. Thank you.

Answers (1)

File APIs for .NET – 25% off Aspose.Total
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more! Use coupon code HOL2016PSB