Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two tables class and category, i want to show all the classes in combobox1 and all categories in combobox2... can anyone help?
Posted
Comments
Richard C Bishop 11-Feb-14 12:53pm    
Where are you stuck? What have you tried?
ArmanKhan19 12-Feb-14 5:30am    
void fillclasscmb()
{
dataadapter.selectcommand = new sqlcommand("Select Classn from class", connection);
datareader dr = new datareader();
while (dr.read())
{
string classnames = dr[0].tostring();
cmb_class.items.add(classnames);
}
}


and same for category and call them in constructor but im getting only class names in cmb_class but not category names in cmb_category.. what should i do??
ZurdoDev 11-Feb-14 12:54pm    
Many of us can help. Where are you stuck?
Sergey Alexandrovich Kryukov 11-Feb-14 13:51pm    
Why could it possibly be a problem? What's the difference, two or one control?
—SA
ArmanKhan19 12-Feb-14 12:24pm    
im getting the data in class combobox by this code
void fillclasscmb()
{
dataadapter.selectcommand = new sqlcommand("Select Classn from class", connection);
datareader dr = new datareader();
while (dr.read())
{
string classnames = dr[0].tostring();
cmb_class.items.add(classnames);
}
}

but im not getting the data from category table in category combobox by this code simultaneously,

1 solution

Here is a Sample Code For Class ComboBox
C#
public DataTable classComboBox()
 {
     DataTable dt = new DataTable();
     SqlConnection conn = new SqlConnection("your connection string");
     SqlCommand comm = new SqlCommand("Select * from class", conn);
     SqlDataAdapter SDA = new SqlDataAdapter(comm);
     SDA.SelectCommand = comm;
     SDA.Fill(dt);
     return dt;
 }

Here is a sample Code For category ComboBox
C#
public DataTable categoryComboBox()
 {
     DataTable dt = new DataTable();
     SqlConnection conn = new SqlConnection("your connection string");
     SqlCommand comm = new SqlCommand("Select * from category ", conn);
     SqlDataAdapter SDA = new SqlDataAdapter(comm);
     SDA.SelectCommand = comm;
     SDA.Fill(dt);
     return dt;
 }

And You Can Call Into Form Like This.......
C#
public void FillClasscombox()
{
    DataTable dt = classComboBox();
    yourClassCombobox.DataSource = dt;
    yourClassCombobox.DisplayMember = "className"; // Here Put Your Display Member Which Yow want display into ComboBox....
    yourClassCombobox.ValueMember = "classId";// Here Put Your value Member

}

Here Your Calling Method Of catogry
C#
public void FillCatogrycombox()
   {
       DataTable dt = categoryComboBox();
       yourClassCombobox.DataSource = dt;
       yourClassCombobox.DisplayMember = "category"; // Here Put Your Display Member Which           Yow want display into ComboBox....
       yourClassCombobox.ValueMember = "categoryId";// Here Put Your value Member

   }


Finaly call FillClasscombox() and FillCatogrycombox() into the Constructor
like this
C#
public yourconstructor()
{
  InitializeComponent();
  FillClasscombox();
   FillCatogrycombox();
}
 
Share this answer
 
v3

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