Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all ,

i have to fill my two combobox1 and combobox2, from datatable column1 and column2. but i do not want to use adapter.plz help me out urgent.

thanks in advance!
Posted
Comments
mkcm2011 8-Nov-11 5:33am    
plz help me asap!!!!!!!!!!!!!

C#
DataTable dt = new DataTable();
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand("select id, name from table", ClsMSetting.Con);
                 rdr = cmd.ExecuteReader();
                dt.TableName = "mydata";
                dt.Load(rdr);
                rdr.Close();
		//cb1 is a combo
		cb1.DisplayMember = "name";
            	cb1.ValueMember = "id";
                
            }
            catch (Exception Ex) { }
 
Share this answer
 
v2
Comments
Menon Santosh 8-Nov-11 5:41am    
ClsMSetting.Con is a connection
mkcm2011 8-Nov-11 5:48am    
if i want for 2 combobox then what shud i do with .Valuemember and.Displaymember
Menon Santosh 8-Nov-11 6:01am    
What do u want, insert same data in combo or diffrent data
mkcm2011 8-Nov-11 6:41am    
SqlCommand mycommand = new SqlCommand("select id, name from tablename", myCon);
SqlDataReader dr = mycommand.ExecuteReader();

while (dr.Read())
{
id_cmb.Items.Add(dr.GetValue(0));
name_cmb.Items.Add(dr.GetValue(1));



its working....for me but i want unique data shud be in my combobox...how shud i do.....plz reply
Menon Santosh 8-Nov-11 6:54am    
Use distinct in query
C#
SqlCommand mycommand = new SqlCommand("select id, name from tablename", myCon);
                SqlDataReader dr = mycommand.ExecuteReader();

                while (dr.Read())
                {
                  id_cmb.Items.Add(dr.GetValue(0));
                  name_cmb.Items.Add(dr.GetValue(1));
}


But its nt working properly.....the combobox are not filling with unique data
 
Share this answer
 
v2
SqlCommand mycommand = new SqlCommand("select id, name from tablename", myCon);
                SqlDataReader dr = mycommand.ExecuteReader();
 
                while (dr.Read())
                {
                  id_cmb.Items.Add(dr.GetValue(0));
                  name_cmb.Items.Add(dr.GetValue(1));
}


Your Above SQL Query not satisfying your condition whatever you want......
If you want to display distinct value in combobox then you will not able to find unique ID for that records (if u have same name with different ID).........So either change your database table with unique Name and ID or don't select ID
select distinct name from tablename
 
Share this answer
 
v2
Comments
mkcm2011 11-Nov-11 1:20am    
then i need to fire two sql query statements in sqlcommand

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