TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Khenne Enyieko
NA
6
6.1k
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.
Reply
Answers (
1
)
refresh datagrid items without losing selected row - XAML
how to share images of Wpf application on Facebook using c#